Skip to content

Commit f33a9fb

Browse files
committed
feat: Add support for specifying NotificationOptions in FastMCP and lowlevel Server creation
1 parent dcc68ce commit f33a9fb

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,9 +833,13 @@ Tools can send logs and notifications through the context:
833833
<!-- snippet-source examples/snippets/servers/notifications.py -->
834834
```python
835835
from mcp.server.fastmcp import Context, FastMCP
836+
from mcp.server.lowlevel.server import NotificationOptions
836837
from mcp.server.session import ServerSession
837838

838-
mcp = FastMCP(name="Notifications Example")
839+
# Setup Server Capabilities
840+
notification_options = NotificationOptions(prompts_changed=False, resources_changed=True, tools_changed=False)
841+
842+
mcp = FastMCP(name="Notifications Example", notification_options=notification_options)
839843

840844

841845
@mcp.tool()

examples/snippets/servers/notifications.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from mcp.server.fastmcp import Context, FastMCP
2+
from mcp.server.lowlevel.server import NotificationOptions
23
from mcp.server.session import ServerSession
34

4-
mcp = FastMCP(name="Notifications Example")
5+
# Setup Server Capabilities
6+
notification_options = NotificationOptions(prompts_changed=False, resources_changed=True, tools_changed=False)
7+
8+
mcp = FastMCP(name="Notifications Example", notification_options=notification_options)
59

610

711
@mcp.tool()

src/mcp/server/fastmcp/server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from mcp.server.fastmcp.utilities.context_injection import find_context_parameter
3434
from mcp.server.fastmcp.utilities.logging import configure_logging, get_logger
3535
from mcp.server.lowlevel.helper_types import ReadResourceContents
36-
from mcp.server.lowlevel.server import LifespanResultT
36+
from mcp.server.lowlevel.server import LifespanResultT, NotificationOptions
3737
from mcp.server.lowlevel.server import Server as MCPServer
3838
from mcp.server.lowlevel.server import lifespan as default_lifespan
3939
from mcp.server.session import ServerSession, ServerSessionT
@@ -148,6 +148,7 @@ def __init__( # noqa: PLR0913
148148
lifespan: Callable[[FastMCP[LifespanResultT]], AbstractAsyncContextManager[LifespanResultT]] | None = None,
149149
auth: AuthSettings | None = None,
150150
transport_security: TransportSecuritySettings | None = None,
151+
notification_options: NotificationOptions | None = None,
151152
):
152153
self.settings = Settings(
153154
debug=debug,
@@ -177,6 +178,7 @@ def __init__( # noqa: PLR0913
177178
# TODO(Marcelo): It seems there's a type mismatch between the lifespan type from an FastMCP and Server.
178179
# We need to create a Lifespan type that is a generic on the server type, like Starlette does.
179180
lifespan=(lifespan_wrapper(self, self.settings.lifespan) if self.settings.lifespan else default_lifespan), # type: ignore
181+
notification_options=notification_options,
180182
)
181183
self._tool_manager = ToolManager(tools=tools, warn_on_duplicate_tools=self.settings.warn_on_duplicate_tools)
182184
self._resource_manager = ResourceManager(warn_on_duplicate_resources=self.settings.warn_on_duplicate_resources)

src/mcp/server/lowlevel/server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,12 @@ def __init__(
142142
[Server[LifespanResultT, RequestT]],
143143
AbstractAsyncContextManager[LifespanResultT],
144144
] = lifespan,
145+
notification_options: NotificationOptions | None = None,
145146
):
146147
self.name = name
147148
self.version = version
148149
self.instructions = instructions
150+
self.notification_options = notification_options or NotificationOptions()
149151
self.website_url = website_url
150152
self.icons = icons
151153
self.lifespan = lifespan
@@ -177,7 +179,7 @@ def pkg_version(package: str) -> str:
177179
server_name=self.name,
178180
server_version=self.version if self.version else pkg_version("mcp"),
179181
capabilities=self.get_capabilities(
180-
notification_options or NotificationOptions(),
182+
notification_options or self.notification_options,
181183
experimental_capabilities or {},
182184
),
183185
instructions=self.instructions,

0 commit comments

Comments
 (0)