Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -888,9 +888,13 @@ Tools can send logs and notifications through the context:
<!-- snippet-source examples/snippets/servers/notifications.py -->
```python
from mcp.server.fastmcp import Context, FastMCP
from mcp.server.lowlevel.server import NotificationOptions
from mcp.server.session import ServerSession

mcp = FastMCP(name="Notifications Example")
# Setup Server Capabilities
notification_options = NotificationOptions(prompts_changed=False, resources_changed=True, tools_changed=False)

mcp = FastMCP(name="Notifications Example", notification_options=notification_options)


@mcp.tool()
Expand Down
6 changes: 5 additions & 1 deletion examples/snippets/servers/notifications.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from mcp.server.fastmcp import Context, FastMCP
from mcp.server.lowlevel.server import NotificationOptions
from mcp.server.session import ServerSession

mcp = FastMCP(name="Notifications Example")
# Setup Server Capabilities
notification_options = NotificationOptions(prompts_changed=False, resources_changed=True, tools_changed=False)

mcp = FastMCP(name="Notifications Example", notification_options=notification_options)


@mcp.tool()
Expand Down
4 changes: 3 additions & 1 deletion src/mcp/server/fastmcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
from mcp.server.fastmcp.utilities.context_injection import find_context_parameter
from mcp.server.fastmcp.utilities.logging import configure_logging, get_logger
from mcp.server.lowlevel.helper_types import ReadResourceContents
from mcp.server.lowlevel.server import LifespanResultT
from mcp.server.lowlevel.server import LifespanResultT, NotificationOptions
from mcp.server.lowlevel.server import Server as MCPServer
from mcp.server.lowlevel.server import lifespan as default_lifespan
from mcp.server.session import ServerSession, ServerSessionT
Expand Down Expand Up @@ -168,6 +168,7 @@ def __init__( # noqa: PLR0913
lifespan: (Callable[[FastMCP[LifespanResultT]], AbstractAsyncContextManager[LifespanResultT]] | None) = None,
auth: AuthSettings | None = None,
transport_security: TransportSecuritySettings | None = None,
notification_options: NotificationOptions | None = None,
):
self.settings = Settings(
debug=debug,
Expand Down Expand Up @@ -197,6 +198,7 @@ def __init__( # noqa: PLR0913
# TODO(Marcelo): It seems there's a type mismatch between the lifespan type from an FastMCP and Server.
# We need to create a Lifespan type that is a generic on the server type, like Starlette does.
lifespan=(lifespan_wrapper(self, self.settings.lifespan) if self.settings.lifespan else default_lifespan), # type: ignore
notification_options=notification_options,
)
self._tool_manager = ToolManager(tools=tools, warn_on_duplicate_tools=self.settings.warn_on_duplicate_tools)
self._resource_manager = ResourceManager(warn_on_duplicate_resources=self.settings.warn_on_duplicate_resources)
Expand Down
4 changes: 3 additions & 1 deletion src/mcp/server/lowlevel/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,12 @@ def __init__(
[Server[LifespanResultT, RequestT]],
AbstractAsyncContextManager[LifespanResultT],
] = lifespan,
notification_options: NotificationOptions | None = None,
):
self.name = name
self.version = version
self.instructions = instructions
self.notification_options = notification_options or NotificationOptions()
self.website_url = website_url
self.icons = icons
self.lifespan = lifespan
Expand Down Expand Up @@ -177,7 +179,7 @@ def pkg_version(package: str) -> str:
server_name=self.name,
server_version=self.version if self.version else pkg_version("mcp"),
capabilities=self.get_capabilities(
notification_options or NotificationOptions(),
notification_options or self.notification_options,
experimental_capabilities or {},
),
instructions=self.instructions,
Expand Down