MCP Tools¶
MCP Tool.
MCPTool
¶
Bases: AsyncBaseTool
MCP Tool Class.
Source code in src/llm_agents_from_scratch/tools/mcp/tool.py
__init__
¶
Initialize an MCP Tool.
Note
It is highly recommended to use MCPToolProvider.get_tools() to
create MCPTool instances. It automatically names tools as
"mcp__{provider_name}__{server_tool_name}" to avoid collisions
across providers. When the tool is invoked, the provider prefix
is stripped to call the tool by its original server-side name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
MCPToolProvider
|
The provider that owns this tool and manages the connection to the MCP server. |
required |
name
|
str
|
The fully qualified tool name. When created via
|
required |
desc
|
str
|
A description of what the tool does. |
required |
params_json_schema
|
dict[str, Any]
|
JSON schema defining the tool's input parameters. |
required |
additional_annotations
|
ToolAnnotations | None
|
Additional MCP tool annotations (hints for clients). Defaults to None. |
None
|
Source code in src/llm_agents_from_scratch/tools/mcp/tool.py
__call__
async
¶
Asynchronously execute the MCP tool call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_call
|
ToolCall
|
The tool call to execute. |
required |
*args
|
Any
|
Additional positional arguments forwarded to the tool. |
()
|
**kwargs
|
Any
|
Additional keyword arguments forwarded to the tool. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
ToolCallResult |
ToolCallResult
|
The tool call result. |
Source code in src/llm_agents_from_scratch/tools/mcp/tool.py
MCP Tool Provider.
MCPToolProvider
¶
MCP Tool Provider class.
Source code in src/llm_agents_from_scratch/tools/mcp/provider.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
__init__
¶
Initialize an MCPToolProvider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
A name identifier for this provider. Used to prefix tool names when creating MCPTool instances (e.g., "mcp__{name}__{tool_name}"). |
required |
stdio_params
|
StdioServerParameters | None
|
Parameters
for connecting to an MCP server via stdio. If both this and
|
None
|
streamable_http_url
|
str | None
|
URL for connecting to
an MCP server via HTTP. Only used if |
None
|
streamable_http_headers
|
dict[str, str] | None
|
HTTP
headers to include with every request to the MCP server (e.g.,
|
None
|
Raises:
| Type | Description |
|---|---|
MissingMCPServerParamsError
|
If neither |
Warns:
| Type | Description |
|---|---|
MCPWarning
|
Emitted if both |
Source code in src/llm_agents_from_scratch/tools/mcp/provider.py
session
async
¶
Get the persistent session.
Returns:
| Name | Type | Description |
|---|---|---|
ClientSession |
ClientSession
|
An initialized MCP client session. |
Raises:
| Type | Description |
|---|---|
Exception
|
Re-raises any exception encountered during session creation (e.g. invalid server path, connection refused). |
Note
This method uses lazy initialization - the session is created on the first call and reused for subsequent calls.
Source code in src/llm_agents_from_scratch/tools/mcp/provider.py
get_tools
async
¶
Fetch tools from the MCP server and create MCPTool instances.
Returns:
| Type | Description |
|---|---|
list[MCPTool]
|
list[MCPTool]: A list of MCPTool instances representing the tools available from the MCP server. |
Source code in src/llm_agents_from_scratch/tools/mcp/provider.py
close
async
¶
Close the persistent session and clean up resources.
Note
For short-lived scripts, calling close() is optional as the OS will clean up subprocess resources when your program exits. For long-running applications, you should call close() to prevent resource leaks.