Add a pluggable server extension API with MCP Apps (#3003)
Co-authored-by: Max Isbey <224885523+maxisbey@users.noreply.github.com>
This commit is contained in:
parent
fe9723853d
commit
4b519782f1
6 changed files with 2598 additions and 24 deletions
0
docs_src/extensions/__init__.py
Normal file
0
docs_src/extensions/__init__.py
Normal file
4
docs_src/extensions/tutorial001.py
Normal file
4
docs_src/extensions/tutorial001.py
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
from mcp.server.apps import Apps
|
||||
from mcp.server.mcpserver import MCPServer
|
||||
|
||||
mcp = MCPServer("demo", extensions=[Apps()])
|
||||
5
docs_src/extensions/tutorial002.py
Normal file
5
docs_src/extensions/tutorial002.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
from mcp.server.extension import Extension
|
||||
|
||||
|
||||
class Stamps(Extension):
|
||||
identifier = "com.example/stamps"
|
||||
35
docs_src/extensions/tutorial003.py
Normal file
35
docs_src/extensions/tutorial003.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
from collections.abc import Sequence
|
||||
from typing import Any
|
||||
|
||||
from mcp import Client
|
||||
from mcp.server.extension import Extension, ToolBinding
|
||||
from mcp.server.mcpserver import MCPServer
|
||||
|
||||
|
||||
def stamp(text: str) -> str:
|
||||
"""Stamp a message with the office seal."""
|
||||
return f"[stamped] {text}"
|
||||
|
||||
|
||||
class Stamps(Extension):
|
||||
"""A purely additive extension: one tool, one capability entry."""
|
||||
|
||||
identifier = "com.example/stamps"
|
||||
|
||||
def settings(self) -> dict[str, Any]:
|
||||
return {"sealed": True}
|
||||
|
||||
def tools(self) -> Sequence[ToolBinding]:
|
||||
return [ToolBinding(fn=stamp)]
|
||||
|
||||
|
||||
mcp = MCPServer("post-office", extensions=[Stamps()])
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
async with Client(mcp) as client:
|
||||
print(client.server_capabilities.extensions)
|
||||
# {'com.example/stamps': {'sealed': True}}
|
||||
result = await client.call_tool("stamp", {"text": "hello"})
|
||||
print(result.content)
|
||||
# [TextContent(text='[stamped] hello')]
|
||||
58
docs_src/extensions/tutorial004.py
Normal file
58
docs_src/extensions/tutorial004.py
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
from collections.abc import Sequence
|
||||
from typing import Any, Literal, cast
|
||||
|
||||
import mcp_types as types
|
||||
from pydantic import Field
|
||||
|
||||
from mcp import Client
|
||||
from mcp.server.context import ServerRequestContext
|
||||
from mcp.server.extension import Extension, MethodBinding
|
||||
from mcp.server.mcpserver import MCPServer, require_client_extension
|
||||
|
||||
EXTENSION_ID = "com.example/search"
|
||||
|
||||
|
||||
class SearchParams(types.RequestParams):
|
||||
query: str
|
||||
limit: int = Field(default=10, ge=1, le=100)
|
||||
|
||||
|
||||
class SearchResult(types.Result):
|
||||
items: list[str]
|
||||
|
||||
|
||||
class SearchRequest(types.Request[SearchParams, Literal["com.example/search"]]):
|
||||
method: Literal["com.example/search"] = "com.example/search"
|
||||
params: SearchParams
|
||||
|
||||
|
||||
async def search(ctx: ServerRequestContext[Any, Any], params: SearchParams) -> SearchResult:
|
||||
require_client_extension(ctx, EXTENSION_ID)
|
||||
return SearchResult(items=[f"{params.query}-{n}" for n in range(params.limit)])
|
||||
|
||||
|
||||
class Search(Extension):
|
||||
"""An extension that serves its own request method."""
|
||||
|
||||
identifier = EXTENSION_ID
|
||||
|
||||
def methods(self) -> Sequence[MethodBinding]:
|
||||
return [
|
||||
MethodBinding(
|
||||
"com.example/search",
|
||||
SearchParams,
|
||||
search,
|
||||
protocol_versions=frozenset({"2026-07-28"}),
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
mcp = MCPServer("catalog", extensions=[Search()])
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
async with Client(mcp, extensions={EXTENSION_ID: {}}) as client:
|
||||
request = SearchRequest(params=SearchParams(query="mcp", limit=3))
|
||||
result = await client.session.send_request(cast("types.ClientRequest", request), SearchResult)
|
||||
print(result.items)
|
||||
# ['mcp-0', 'mcp-1', 'mcp-2']
|
||||
34
docs_src/extensions/tutorial005.py
Normal file
34
docs_src/extensions/tutorial005.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import logging
|
||||
from typing import Any
|
||||
|
||||
from mcp_types import CallToolRequestParams
|
||||
|
||||
from mcp.server.context import CallNext, HandlerResult, ServerRequestContext
|
||||
from mcp.server.extension import Extension
|
||||
from mcp.server.mcpserver import MCPServer
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class AuditLog(Extension):
|
||||
"""Observe every tools/call without touching its result."""
|
||||
|
||||
identifier = "com.example/audit"
|
||||
|
||||
async def intercept_tool_call(
|
||||
self,
|
||||
params: CallToolRequestParams,
|
||||
ctx: ServerRequestContext[Any, Any],
|
||||
call_next: CallNext,
|
||||
) -> HandlerResult:
|
||||
logger.info("tool %r called", params.name)
|
||||
return await call_next(ctx)
|
||||
|
||||
|
||||
mcp = MCPServer("audited", extensions=[AuditLog()])
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def add(a: int, b: int) -> int:
|
||||
"""Add two numbers."""
|
||||
return a + b
|
||||
Loading…
Reference in a new issue