Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547"""Tests for the FastMCP-native server extension API (SEP-2133).
A synthetic extension exercises every contribution kind: capabilityadvertisement, additive request methods (with protocol-version gating),tools/call interception (observe and short-circuit), a lifespan hook (order andmounted-server behaviour), and the per-request capability sniff. Registrationguards (duplicate identifier, spec-method rejection, invalid identifier) and azero-behaviour-change baseline round it out."""
from __future__ import annotations
from contextlib import asynccontextmanagerfrom typing import Any, Literal, cast
import mcp_typesimport pytestfrom mcp.server.context import ServerRequestContextfrom mcp.shared.exceptions import MCPErrorfrom mcp_types import CLIENT_CAPABILITIES_META_KEY, METHOD_NOT_FOUND, RequestParams
from fastmcp import FastMCPfrom fastmcp.client import Clientfrom fastmcp.server.extensions import ( MethodBinding, ServerExtension, read_client_extension_settings,)from fastmcp.tools.base import ToolResult
EXT_ID = "com.example/synthetic"
class PingParams(RequestParams): echo: str | None = None
class PingRequest(mcp_types.Request): method: Literal["synthetic/ping"] = "synthetic/ping" params: PingParams
class PingResult(mcp_types.Result): pong: bool echo: str | None = None
def _text(result: mcp_types.CallToolResult) -> str: block = result.content[0] assert isinstance(block, mcp_types.TextContent) return block.text
# ---------------------------------------------------------------------------# Capability advertisement# ---------------------------------------------------------------------------
async def test_capability_advertised_to_modern_client(): """A registered extension's settings appear under capabilities.extensions.
Uses ``mode='auto'`` so the client negotiates the modern era via ``server/discover`` (which reads ``get_capabilities`` directly); the SDK's version sieve strips ``capabilities.extensions`` only on legacy eras. """
class Ext(ServerExtension): identifier = EXT_ID
def settings(self) -> dict[str, Any]: return {"version": "1"}
mcp = FastMCP("t") mcp.add_extension(Ext())
async with Client(mcp, mode="auto") as client: extensions = client.server_capabilities.extensions or {} assert extensions.get(EXT_ID) == {"version": "1"}
async def test_capability_absent_without_registration(): """A server with no extensions advertises none of its own.""" mcp = FastMCP("t") async with Client(mcp, mode="auto") as client: extensions = client.server_capabilities.extensions or {} assert EXT_ID not in extensions
async def test_empty_settings_still_advertise(): """The default empty-settings extension is advertised with an empty dict."""
class Ext(ServerExtension): identifier = EXT_ID
mcp = FastMCP("t") mcp.add_extension(Ext()) async with Client(mcp, mode="auto") as client: extensions = client.server_capabilities.extensions or {} assert extensions.get(EXT_ID) == {}
# ---------------------------------------------------------------------------# Additive request methods# ---------------------------------------------------------------------------
class _PingExtension(ServerExtension): identifier = EXT_ID
def methods(self) -> list[MethodBinding]: async def handler( ctx: ServerRequestContext[Any, Any], params: PingParams ) -> PingResult: return PingResult(pong=True, echo=params.echo)
return [ MethodBinding( method="synthetic/ping", params_type=PingParams, handler=handler, ) ]
async def test_custom_method_callable_end_to_end(): mcp = FastMCP("t") mcp.add_extension(_PingExtension())
async with Client(mcp, mode="auto") as client: result = await client.session.send_request( request=PingRequest(params=PingParams(echo="hi")), result_type=PingResult, ) assert result.pong is True assert result.echo == "hi"
async def test_method_handler_reaches_server_registry(): """A method handler can reach the FastMCP component registry via the extension."""
class Ext(ServerExtension): identifier = EXT_ID
def methods(self) -> list[MethodBinding]: async def handler( ctx: ServerRequestContext[Any, Any], params: PingParams ) -> PingResult: tools = await self.server.list_tools() return PingResult(pong=len(tools) == 1)
return [ MethodBinding( method="synthetic/ping", params_type=PingParams, handler=handler, ) ]
mcp = FastMCP("t")
@mcp.tool def only_tool() -> str: return "x"
mcp.add_extension(Ext()) async with Client(mcp, mode="auto") as client: result = await client.session.send_request( request=PingRequest(params=PingParams()), result_type=PingResult, ) assert result.pong is True
async def test_method_protocol_version_gating(): """A version-gated method is rejected as METHOD_NOT_FOUND off its versions."""
class Ext(ServerExtension): identifier = EXT_ID
def methods(self) -> list[MethodBinding]: async def handler( ctx: ServerRequestContext[Any, Any], params: PingParams ) -> PingResult: return PingResult(pong=True)
return [ MethodBinding( method="synthetic/ping", params_type=PingParams, handler=handler, protocol_versions=frozenset({"2026-07-28"}), ) ]
mcp = FastMCP("t") mcp.add_extension(Ext())
async with Client(mcp, mode="legacy") as client: with pytest.raises(MCPError) as exc_info: await client.session.send_request( request=PingRequest(params=PingParams()), result_type=PingResult, ) assert exc_info.value.error.code == METHOD_NOT_FOUND
# ---------------------------------------------------------------------------# tools/call interception# ---------------------------------------------------------------------------
async def test_interceptor_observes_tool_call(): """A pass-through interceptor sees the call and the tool still runs.""" seen: list[str] = []
class Ext(ServerExtension): identifier = EXT_ID
async def intercept_tool_call(self, params, context, call_next): seen.append(params.name) return await call_next()
mcp = FastMCP("t")
@mcp.tool def greet() -> str: return "hello"
mcp.add_extension(Ext()) async with Client(mcp, mode="auto") as client: result = await client.call_tool("greet") assert _text(result) == "hello" assert seen == ["greet"]
async def test_interceptor_short_circuits(): """An interceptor can return its own result without running the tool body.""" ran = []
class Ext(ServerExtension): identifier = EXT_ID
async def intercept_tool_call(self, params, context, call_next): return ToolResult( content=[mcp_types.TextContent(type="text", text="intercepted")] )
mcp = FastMCP("t")
@mcp.tool def greet(): ran.append(True) return "hello"
mcp.add_extension(Ext()) async with Client(mcp, mode="auto") as client: result = await client.call_tool("greet") assert _text(result) == "intercepted" assert ran == []
async def test_interceptor_reaches_tool_metadata(): """An interceptor can resolve the tool being called through the context.""" captured: dict[str, Any] = {}
class Ext(ServerExtension): identifier = EXT_ID
async def intercept_tool_call(self, params, context, call_next): tool = await context.fastmcp.get_tool(params.name) captured["title"] = tool.title return await call_next()
mcp = FastMCP("t")
@mcp.tool(title="A Greeting") def greet() -> str: return "hello"
mcp.add_extension(Ext()) async with Client(mcp, mode="auto") as client: await client.call_tool("greet") assert captured["title"] == "A Greeting"
async def test_interceptors_nest_first_registered_outermost(): """Multiple interceptors nest with the first-registered extension outermost.""" order: list[str] = []
def make_ext(identifier: str, label: str) -> ServerExtension: class Ext(ServerExtension): async def intercept_tool_call(self, params, context, call_next): order.append(f"{label}-before") result = await call_next() order.append(f"{label}-after") return result
ext = Ext() ext.identifier = identifier return ext
mcp = FastMCP("t")
@mcp.tool def greet() -> str: return "hello"
mcp.add_extension(make_ext("com.example/outer", "outer")) mcp.add_extension(make_ext("com.example/inner", "inner")) async with Client(mcp, mode="auto") as client: await client.call_tool("greet")
assert order == ["outer-before", "inner-before", "inner-after", "outer-after"]
async def test_no_extensions_leaves_tool_call_unchanged(): """With no extensions registered, tools/call behaves exactly as before.""" mcp = FastMCP("t")
@mcp.tool def greet() -> str: return "hello"
assert mcp._extensions == {} async with Client(mcp, mode="auto") as client: result = await client.call_tool("greet") assert _text(result) == "hello"
# ---------------------------------------------------------------------------# Lifespan hook# ---------------------------------------------------------------------------
def _recording_extension(identifier: str, log: list[str]) -> ServerExtension: class Ext(ServerExtension): @asynccontextmanager async def lifespan(self): log.append(f"{identifier}:enter") try: yield finally: log.append(f"{identifier}:exit")
ext = Ext() ext.identifier = identifier return ext
async def test_lifespan_entered_and_exited(): log: list[str] = [] mcp = FastMCP("t") mcp.add_extension(_recording_extension(EXT_ID, log))
async with Client(mcp, mode="auto"): assert log == [f"{EXT_ID}:enter"] assert log == [f"{EXT_ID}:enter", f"{EXT_ID}:exit"]
async def test_lifespans_enter_in_order_exit_in_reverse(): log: list[str] = [] mcp = FastMCP("t") mcp.add_extension(_recording_extension("com.example/a", log)) mcp.add_extension(_recording_extension("com.example/b", log))
async with Client(mcp, mode="auto"): pass
assert log == [ "com.example/a:enter", "com.example/b:enter", "com.example/b:exit", "com.example/a:exit", ]
async def test_standalone_server_enters_extension_lifespan(): log: list[str] = [] child = FastMCP("child") child.add_extension(_recording_extension(EXT_ID, log))
async with Client(child, mode="auto"): assert log == [f"{EXT_ID}:enter"] assert log == [f"{EXT_ID}:enter", f"{EXT_ID}:exit"]
async def test_mounted_child_defers_extension_lifespan_to_root(): """A mounted child's extension lifespan is not entered below a root.
Mirrors the shared Docket: extension lifespans that may start shared infrastructure are owned by the tree root, so a mounted child defers. """ log: list[str] = [] child = FastMCP("child") child.add_extension(_recording_extension(EXT_ID, log))
root = FastMCP("root") root.mount(child)
async with Client(root, mode="auto"): pass
assert log == []
# ---------------------------------------------------------------------------# Request-time capability sniff# ---------------------------------------------------------------------------
def _ctx_with_meta(meta: dict[str, Any] | None) -> ServerRequestContext[Any, Any]: params: dict[str, Any] = {} if meta is not None: params["_meta"] = meta return ServerRequestContext( session=cast(Any, object()), lifespan_context={}, protocol_version="2026-07-28", method="synthetic/ping", params=params, )
def test_capability_sniff_reads_declared_settings(): meta = { CLIENT_CAPABILITIES_META_KEY: { "extensions": {EXT_ID: {"limit": 5}}, } } assert read_client_extension_settings(_ctx_with_meta(meta), EXT_ID) == {"limit": 5}
def test_capability_sniff_empty_settings_is_opt_in(): """An empty settings dict is a valid opt-in, distinct from absence (None).""" meta = {CLIENT_CAPABILITIES_META_KEY: {"extensions": {EXT_ID: {}}}} assert read_client_extension_settings(_ctx_with_meta(meta), EXT_ID) == {}
@pytest.mark.parametrize( "meta", [ None, {}, {CLIENT_CAPABILITIES_META_KEY: {}}, {CLIENT_CAPABILITIES_META_KEY: {"extensions": {"other/ext": {}}}}, ],)def test_capability_sniff_returns_none_when_not_opted_in(meta): assert read_client_extension_settings(_ctx_with_meta(meta), EXT_ID) is None
def test_client_settings_convenience_uses_own_identifier(): class Ext(ServerExtension): identifier = EXT_ID
meta = {CLIENT_CAPABILITIES_META_KEY: {"extensions": {EXT_ID: {"a": 1}}}} assert Ext().client_settings(_ctx_with_meta(meta)) == {"a": 1}
# ---------------------------------------------------------------------------# Registration guards# ---------------------------------------------------------------------------
async def test_duplicate_identifier_rejected(): class Ext(ServerExtension): identifier = EXT_ID
mcp = FastMCP("t") mcp.add_extension(Ext()) with pytest.raises(ValueError, match="already registered"): mcp.add_extension(Ext())
async def test_registration_after_lifespan_start_rejected(): """Registering once the server is serving would skip the extension's lifespan, leaving it silently half-active — so it raises instead."""
class Ext(ServerExtension): identifier = EXT_ID
mcp = FastMCP("t") async with Client(mcp, mode="auto"): with pytest.raises(RuntimeError, match="lifespan has already started"): mcp.add_extension(Ext())
def test_spec_method_name_rejected(): async def handler(ctx: Any, params: Any) -> None: return None
with pytest.raises(ValueError, match="spec method"): MethodBinding( method="tools/call", params_type=PingParams, handler=handler, )
def test_empty_protocol_versions_rejected(): async def handler(ctx: Any, params: Any) -> None: return None
with pytest.raises(ValueError, match="protocol_versions"): MethodBinding( method="synthetic/ping", params_type=PingParams, handler=handler, protocol_versions=frozenset(), )
def test_invalid_identifier_rejected_at_class_definition(): with pytest.raises(TypeError, match="reverse-DNS"):
class Ext(ServerExtension): identifier = "no-prefix"
async def test_per_instance_invalid_identifier_rejected_at_registration(): class Ext(ServerExtension): pass
ext = Ext() ext.identifier = "no-prefix" mcp = FastMCP("t") with pytest.raises(TypeError, match="reverse-DNS"): mcp.add_extension(ext)
def test_bound_server_accessible_after_registration(): class Ext(ServerExtension): identifier = EXT_ID
ext = Ext() mcp = FastMCP("t") mcp.add_extension(ext) assert ext.server is mcp
def test_unbound_server_access_raises(): class Ext(ServerExtension): identifier = EXT_ID
with pytest.raises(RuntimeError, match="not bound"): _ = Ext().server