Add the client-side subscriptions/listen driver (#3047)
This commit is contained in:
parent
6d2e908f2b
commit
9bdc03d54e
1 changed files with 2587 additions and 295 deletions
|
|
@ -7,34 +7,43 @@ from mcp.server.lowlevel import Server
|
|||
from mcp.server.subscriptions import InMemorySubscriptionBus, ListenHandler, ResourceUpdated
|
||||
|
||||
bus = InMemorySubscriptionBus()
|
||||
listen_handler = ListenHandler(bus)
|
||||
|
||||
NOTES = {"todo": "buy milk"}
|
||||
BOARD = {"design": False, "build": False}
|
||||
|
||||
EDIT_NOTE_SCHEMA: dict[str, Any] = {
|
||||
COMPLETE_TASK_SCHEMA: dict[str, Any] = {
|
||||
"type": "object",
|
||||
"properties": {"name": {"type": "string"}, "text": {"type": "string"}},
|
||||
"required": ["name", "text"],
|
||||
"properties": {"task": {"type": "string"}},
|
||||
"required": ["task"],
|
||||
}
|
||||
|
||||
|
||||
async def read_resource(
|
||||
ctx: ServerRequestContext[Any], params: types.ReadResourceRequestParams
|
||||
) -> types.ReadResourceResult:
|
||||
board = "\n".join(f"[{'x' if done else ' '}] {task}" for task, done in BOARD.items())
|
||||
return types.ReadResourceResult(contents=[types.TextResourceContents(uri=params.uri, text=board)])
|
||||
|
||||
|
||||
async def list_tools(
|
||||
ctx: ServerRequestContext[Any], params: types.PaginatedRequestParams | None
|
||||
) -> types.ListToolsResult:
|
||||
return types.ListToolsResult(
|
||||
tools=[types.Tool(name="edit_note", description="Replace a note's text.", input_schema=EDIT_NOTE_SCHEMA)]
|
||||
tools=[types.Tool(name="complete_task", description="Mark a task done.", input_schema=COMPLETE_TASK_SCHEMA)]
|
||||
)
|
||||
|
||||
|
||||
async def call_tool(ctx: ServerRequestContext[Any], params: types.CallToolRequestParams) -> types.CallToolResult:
|
||||
args = params.arguments or {}
|
||||
NOTES[args["name"]] = args["text"]
|
||||
await bus.publish(ResourceUpdated(uri=f"note://{args['name']}"))
|
||||
return types.CallToolResult(content=[types.TextContent(type="text", text="saved")])
|
||||
BOARD[args["task"]] = True
|
||||
await bus.publish(ResourceUpdated(uri="board://sprint"))
|
||||
return types.CallToolResult(content=[types.TextContent(type="text", text="done")])
|
||||
|
||||
|
||||
server = Server(
|
||||
"notebook",
|
||||
"sprint-board",
|
||||
on_read_resource=read_resource,
|
||||
on_list_tools=list_tools,
|
||||
on_call_tool=call_tool,
|
||||
on_subscriptions_listen=ListenHandler(bus),
|
||||
on_subscriptions_listen=listen_handler,
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue