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
1 changed files with 2598 additions and 24 deletions
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')]
|
||||
Loading…
Reference in a new issue