From 631c58960f6d02cf200fd6d5f18c4d27a4f90ed0 Mon Sep 17 00:00:00 2001 From: David Soria Parra Date: Fri, 11 Oct 2024 16:56:25 +0100 Subject: [PATCH] Add support for list_tools --- mcp_python/server/__init__.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/mcp_python/server/__init__.py b/mcp_python/server/__init__.py index 3339db2..c6b73cb 100644 --- a/mcp_python/server/__init__.py +++ b/mcp_python/server/__init__.py @@ -24,6 +24,8 @@ ListPromptsResult, ListResourcesRequest, ListResourcesResult, + ListToolsRequest, + ListToolsResult, LoggingLevel, ProgressNotification, Prompt, @@ -36,6 +38,7 @@ ServerResult, SetLevelRequest, SubscribeRequest, + Tool, UnsubscribeRequest, ) @@ -79,7 +82,7 @@ def get_capability(req_type: type) -> dict[str, Any] | None: return ServerCapabilities( prompts=get_capability(ListPromptsRequest), resources=get_capability(ListResourcesRequest), - tools=get_capability(ListPromptsRequest), + tools=get_capability(ListToolsRequest), logging=get_capability(SetLevelRequest), ) @@ -205,6 +208,7 @@ async def handler(req: ReadResourceRequest): return decorator + def set_logging_level(self): from mcp_python.types import EmptyResult @@ -250,6 +254,19 @@ async def handler(req: UnsubscribeRequest): return decorator + def list_tools(self): + def decorator(func: Callable[[], Awaitable[list[Tool]]]): + logger.debug("Registering handler for ListToolsRequest") + + async def handler(_: Any): + tools = await func() + return ServerResult(ListToolsResult(tools=tools)) + + self.request_handlers[ListToolsRequest] = handler + return func + + return decorator + def call_tool(self): from mcp_python.types import CallToolResult