33from __future__ import annotations as _annotations
44
55import inspect
6- import json
76import re
87from collections .abc import AsyncIterator , Awaitable , Callable , Iterable , Sequence
98from contextlib import (
5554 GetPromptResult ,
5655 ImageContent ,
5756 TextContent ,
57+ ToolAnnotations ,
5858)
5959from mcp .types import Prompt as MCPPrompt
6060from mcp .types import PromptArgument as MCPPromptArgument
@@ -210,6 +210,7 @@ async def list_tools(self) -> list[MCPTool]:
210210 name = info .name ,
211211 description = info .description ,
212212 inputSchema = info .parameters ,
213+ annotations = info .annotations ,
213214 )
214215 for info in tools
215216 ]
@@ -278,6 +279,7 @@ def add_tool(
278279 fn : AnyFunction ,
279280 name : str | None = None ,
280281 description : str | None = None ,
282+ annotations : ToolAnnotations | None = None ,
281283 ) -> None :
282284 """Add a tool to the server.
283285
@@ -288,11 +290,17 @@ def add_tool(
288290 fn: The function to register as a tool
289291 name: Optional name for the tool (defaults to function name)
290292 description: Optional description of what the tool does
293+ annotations: Optional ToolAnnotations providing additional tool information
291294 """
292- self ._tool_manager .add_tool (fn , name = name , description = description )
295+ self ._tool_manager .add_tool (
296+ fn , name = name , description = description , annotations = annotations
297+ )
293298
294299 def tool (
295- self , name : str | None = None , description : str | None = None
300+ self ,
301+ name : str | None = None ,
302+ description : str | None = None ,
303+ annotations : ToolAnnotations | None = None ,
296304 ) -> Callable [[AnyFunction ], AnyFunction ]:
297305 """Decorator to register a tool.
298306
@@ -303,6 +311,7 @@ def tool(
303311 Args:
304312 name: Optional name for the tool (defaults to function name)
305313 description: Optional description of what the tool does
314+ annotations: Optional ToolAnnotations providing additional tool information
306315
307316 Example:
308317 @server.tool()
@@ -327,7 +336,9 @@ async def async_tool(x: int, context: Context) -> str:
327336 )
328337
329338 def decorator (fn : AnyFunction ) -> AnyFunction :
330- self .add_tool (fn , name = name , description = description )
339+ self .add_tool (
340+ fn , name = name , description = description , annotations = annotations
341+ )
331342 return fn
332343
333344 return decorator
@@ -685,10 +696,7 @@ def _convert_to_content(
685696 return list (chain .from_iterable (_convert_to_content (item ) for item in result )) # type: ignore[reportUnknownVariableType]
686697
687698 if not isinstance (result , str ):
688- try :
689- result = json .dumps (pydantic_core .to_jsonable_python (result ))
690- except Exception :
691- result = str (result )
699+ result = pydantic_core .to_json (result , fallback = str , indent = 2 ).decode ()
692700
693701 return [TextContent (type = "text" , text = result )]
694702
0 commit comments