Core
Core module for the MCPAdapt library.
This module contains the core functionality for the MCPAdapt library. It provides the basic interfaces and classes for adapting tools from MCP to the desired Agent framework.
ToolAdapter
Bases: ABC
A basic interface for adapting tools from MCP to the desired Agent framework.
Source code in src/mcpadapt/core.py
adapt
abstractmethod
Adapt a single tool from MCP to the desired Agent framework.
The MCP protocol will provide a name, description and inputSchema in JSON Schema format. This needs to be adapted to the desired Agent framework.
Note that the function is synchronous (not a coroutine) you can use
:meth:ToolAdapter.async_adapt
if you need to use the tool asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Callable[[dict | None], CallToolResult]
|
The function to be called (will call the tool via the MCP protocol). |
required |
mcp_tool
|
Tool
|
The tool to adapt. |
required |
Returns:
Type | Description |
---|---|
Any
|
The adapted tool in the agentic framework of choice. |
Source code in src/mcpadapt/core.py
async_adapt
async_adapt(
afunc: Callable[
[dict | None], Coroutine[Any, Any, CallToolResult]
],
mcp_tool: Tool,
) -> Any
Adapt a single tool from MCP to the desired Agent framework.
The MCP protocol will provide a name, description and inputSchema in JSON Schema format. This needs to be adapted to the desired Agent framework.
Note that the function is asynchronous (a coroutine) you can use
:meth:ToolAdapter.adapt
if you need to use the tool synchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
afunc
|
Callable[[dict | None], Coroutine[Any, Any, CallToolResult]]
|
The coroutine to be called. |
required |
mcp_tool
|
Tool
|
The tool to adapt. |
required |
Returns:
Type | Description |
---|---|
Any
|
The adapted tool in the agentic framework of choice. |
Source code in src/mcpadapt/core.py
MCPAdapt
The main class for adapting MCP tools to the desired Agent framework.
This class can be used either as a sync or async context manager.
If running synchronously, it will run the MCP server in a separate thread and take care of making the tools synchronous without blocking the server.
If running asynchronously, it will use the async context manager and return async tools.
Dependening on what your Agent framework supports choose the approriate method. If async is supported it is recommended.
Important Note: adapters need to implement the async_adapt method to support async tools.
Usage:
sync usage
with MCPAdapt(StdioServerParameters(command="uv", args=["run", "src/echo.py"]), SmolAgentAdapter()) as tools: print(tools)
async usage
async with MCPAdapt(StdioServerParameters(command="uv", args=["run", "src/echo.py"]), SmolAgentAdapter()) as tools: print(tools)
async usage with sse
async with MCPAdapt({"host": "127.0.0.1", "port": 8000}, SmolAgentAdapter()) as tools: print(tools)
Source code in src/mcpadapt/core.py
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
|
__init__
__init__(
serverparams: StdioServerParameters | dict[str, Any],
adapter: ToolAdapter,
connect_timeout: int = 30,
)
Manage the MCP server / client lifecycle and expose tools adapted with the adapter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
serverparams
|
StdioServerParameters | dict[str, Any]
|
MCP server parameters (stdio or sse). |
required |
adapter
|
ToolAdapter
|
Adapter to use to convert MCP tools call into agentic framework tools. |
required |
connect_timeout
|
int
|
Connection timeout in seconds to the mcp server (default is 30s). |
30
|
Raises:
Type | Description |
---|---|
TimeoutError
|
When the connection to the mcp server time out. |
Source code in src/mcpadapt/core.py
tools
Returns the tools from the MCP server adapted to the desired Agent framework.
This is what is yielded if used as a context manager otherwise you can access it directly via this method.
An equivalent async method is available if your Agent framework supports it:
see :meth:atools
.
Source code in src/mcpadapt/core.py
close
Clean up resources and stop the client.
Source code in src/mcpadapt/core.py
atools
Returns the tools from the MCP server adapted to the desired Agent framework.
This is what is yielded if used as an async context manager otherwise you can access it directly via this method.
An equivalent async method is available if your Agent framework supports it:
see :meth:atools
.
Source code in src/mcpadapt/core.py
mcptools
async
mcptools(
serverparams: StdioServerParameters | dict[str, Any],
) -> tuple[ClientSession, list[Tool]]
Async context manager that yields tools from an MCP server.
Note: the session can be then used to call tools on the MCP server but it's async. Use MCPAdapt instead if you need to use the tools synchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
serverparams
|
StdioServerParameters | dict[str, Any]
|
Parameters passed to either the stdio client or sse client. * if StdioServerParameters, run the MCP server using the stdio protocol. * if dict, assume the dict corresponds to parameters to an sse MCP server. |
required |
Yields:
Type | Description |
---|---|
tuple[ClientSession, list[Tool]]
|
A list of tools available on the MCP server. |
Usage:
async with mcptools(StdioServerParameters(command="uv", args=["run", "src/echo.py"])) as (session, tools): print(tools)