MCPcopy Index your code
hub / github.com/modelcontextprotocol/python-sdk / from_function

Method from_function

src/mcp/server/mcpserver/tools/base.py:44–89  ·  view source on GitHub ↗

Create a Tool from a function.

(
        cls,
        fn: Callable[..., Any],
        name: str | None = None,
        title: str | None = None,
        description: str | None = None,
        context_kwarg: str | None = None,
        annotations: ToolAnnotations | None = None,
        icons: list[Icon] | None = None,
        meta: dict[str, Any] | None = None,
        structured_output: bool | None = None,
    )

Source from the content-addressed store, hash-verified

42
43 @classmethod
44 def from_function(
45 cls,
46 fn: Callable[..., Any],
47 name: str | None = None,
48 title: str | None = None,
49 description: str | None = None,
50 context_kwarg: str | None = None,
51 annotations: ToolAnnotations | None = None,
52 icons: list[Icon] | None = None,
53 meta: dict[str, Any] | None = None,
54 structured_output: bool | None = None,
55 ) -> Tool:
56 """Create a Tool from a function."""
57 func_name = name or fn.__name__
58
59 validate_and_warn_tool_name(func_name)
60
61 if func_name == "<lambda>":
62 raise ValueError("You must provide a name for lambda functions")
63
64 func_doc = description or fn.__doc__ or ""
65 is_async = is_async_callable(fn)
66
67 if context_kwarg is None: # pragma: no branch
68 context_kwarg = find_context_parameter(fn)
69
70 func_arg_metadata = func_metadata(
71 fn,
72 skip_names=[context_kwarg] if context_kwarg is not None else [],
73 structured_output=structured_output,
74 )
75 parameters = func_arg_metadata.arg_model.model_json_schema(by_alias=True)
76
77 return cls(
78 fn=fn,
79 name=func_name,
80 title=title,
81 description=func_doc,
82 parameters=parameters,
83 fn_metadata=func_arg_metadata,
84 is_async=is_async,
85 context_kwarg=context_kwarg,
86 annotations=annotations,
87 icons=icons,
88 meta=meta,
89 )
90
91 async def run(
92 self,

Calls 4

is_async_callableFunction · 0.90
find_context_parameterFunction · 0.90
func_metadataFunction · 0.90