Mcp
McpToolSpec #
Bases: BaseToolSpec, TypeResolutionMixin, TypeCreationMixin, FieldExtractionMixin
MCPToolSpec will get the tools from MCP Client (only need to implement ClientSession) and convert them to LlamaIndex's FunctionTool objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
ClientSession
|
An MCP client instance implementing ClientSession, and it should support the following methods in ClientSession: - list_tools: List all tools. - call_tool: Call a tool. - list_resources: List all resources. - read_resource: Read a resource. |
required |
allowed_tools
|
Optional[List[str]]
|
If set, only return tools with the specified names. |
None
|
include_resources
|
bool
|
Whether to include resources in the tool list. |
False
|
Source code in llama_index/tools/mcp/base.py
19 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 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 | |
fetch_tools
async
#
fetch_tools() -> List[Any]
An asynchronous method to get the tools list from MCP Client. If allowed_tools is set, it will filter the tools.
Returns:
| Type | Description |
|---|---|
List[Any]
|
A list of tools, each tool object needs to contain name, description, inputSchema properties. |
Source code in llama_index/tools/mcp/base.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |
fetch_resources
async
#
fetch_resources() -> List[Resource]
An asynchronous method to get the resources list from MCP Client.
Source code in llama_index/tools/mcp/base.py
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 | |
to_tool_list_async
async
#
to_tool_list_async() -> List[FunctionTool]
Asynchronous method to convert MCP tools to FunctionTool objects.
Returns:
| Type | Description |
|---|---|
List[FunctionTool]
|
A list of FunctionTool objects. |
Source code in llama_index/tools/mcp/base.py
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 | |
to_tool_list #
to_tool_list() -> List[FunctionTool]
Synchronous interface: Convert MCP Client tools to FunctionTool objects. Note: This method should not be called in an asynchronous environment, otherwise an exception will be thrown. Use to_tool_list_async instead.
Returns:
| Type | Description |
|---|---|
List[FunctionTool]
|
A list of FunctionTool objects. |
Source code in llama_index/tools/mcp/base.py
164 165 166 167 168 169 170 171 172 173 | |
create_model_from_json_schema #
create_model_from_json_schema(schema: dict[str, Any], model_name: str = 'DynamicModel') -> type[BaseModel]
To create a Pydantic model from the JSON Schema of MCP tools.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict[str, Any]
|
A JSON Schema dictionary containing properties and required fields. |
required |
model_name
|
str
|
The name of the model. |
'DynamicModel'
|
Returns:
| Type | Description |
|---|---|
type[BaseModel]
|
A Pydantic model class. |
Source code in llama_index/tools/mcp/base.py
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 | |
BasicMCPClient #
Bases: ClientSession
Basic MCP client that can be used to connect to an MCP server.
This is useful for connecting to any MCP server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command_or_url
|
str
|
The command to run or the URL to connect to. |
required |
args
|
Optional[List[str]]
|
The arguments to pass to StdioServerParameters. |
None
|
env
|
Optional[Dict[str, str]]
|
The environment variables to set for StdioServerParameters. |
None
|
timeout
|
int
|
The timeout for the command in seconds. |
30
|
auth
|
Optional[OAuthClientProvider]
|
Optional OAuth client provider for authentication. |
None
|
sampling_callback
|
Optional[Callable[[CreateMessageRequestParams], Awaitable[CreateMessageResult]]]
|
Optional callback for handling sampling messages. |
None
|
headers
|
Optional[Dict[str, Any]]
|
Optional headers to pass by sse client or streamable http client |
None
|
tool_call_logs_callback
|
Optional[Callable[[List[str]], Awaitable[Any]]]
|
Async function to store the logs deriving from an MCP tool call: logs are provided as a list of strings, representing log messages. Defaults to None. |
None
|
Source code in llama_index/tools/mcp/client.py
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 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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
with_oauth
classmethod
#
with_oauth(command_or_url: str, client_name: str, redirect_uris: List[str], redirect_handler: Callable[[str], None], callback_handler: Callable[[], Tuple[str, Optional[str]]], args: Optional[List[str]] = None, env: Optional[Dict[str, str]] = None, timeout: int = 30, token_storage: Optional[TokenStorage] = None, tool_call_logs_callback: Optional[Callable[[List[str]], Awaitable[Any]]] = None) -> BasicMCPClient
Create a client with OAuth authentication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command_or_url
|
str
|
The command to run or the URL to connect to |
required |
client_name
|
str
|
The name of the OAuth client |
required |
redirect_uris
|
List[str]
|
The redirect URIs for the OAuth flow |
required |
redirect_handler
|
Callable[[str], None]
|
Function that handles the redirect URL |
required |
callback_handler
|
Callable[[], Tuple[str, Optional[str]]]
|
Function that returns the auth code and state |
required |
token_storage
|
Optional[TokenStorage]
|
Optional token storage for OAuth client. If not provided, a default in-memory storage is used (tokens will be lost on restart). |
None
|
args
|
Optional[List[str]]
|
The arguments to pass to StdioServerParameters. |
None
|
env
|
Optional[Dict[str, str]]
|
The environment variables to set for StdioServerParameters. |
None
|
timeout
|
int
|
The timeout for the command in seconds. |
30
|
tool_call_logs_callback
|
Optional[Callable[[List[str]], Awaitable[Any]]]
|
Async function to store the logs deriving from an MCP tool call: logs are provided as a list of strings, representing log messages. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
BasicMCPClient
|
An authenticated MCP client |
Source code in llama_index/tools/mcp/client.py
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 | |
call_tool
async
#
call_tool(tool_name: str, arguments: Optional[dict] = None, progress_callback: Optional[ProgressFnT] = None) -> CallToolResult
Call a tool on the MCP server.
Source code in llama_index/tools/mcp/client.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
list_tools
async
#
list_tools() -> ListToolsResult
List all available tools on the MCP server.
Source code in llama_index/tools/mcp/client.py
302 303 304 305 | |
list_resources
async
#
list_resources() -> ListToolsResult
List all available resources on the MCP server.
Source code in llama_index/tools/mcp/client.py
308 309 310 311 | |
list_resource_templates
async
#
list_resource_templates() -> ListToolsResult
List all dynamic available resources on the MCP server.
Source code in llama_index/tools/mcp/client.py
313 314 315 316 | |
read_resource
async
#
read_resource(resource_uri: AnyUrl) -> ReadResourceResult
Read a resource from the MCP server.
Returns:
| Type | Description |
|---|---|
ReadResourceResult
|
Tuple containing the resource content as bytes and the MIME type |
Source code in llama_index/tools/mcp/client.py
318 319 320 321 322 323 324 325 326 327 | |
list_prompts
async
#
list_prompts() -> List[Prompt]
List all available prompts on the MCP server.
Source code in llama_index/tools/mcp/client.py
331 332 333 334 | |
get_prompt
async
#
get_prompt(prompt_name: str, arguments: Optional[Dict[str, str]] = None) -> List[ChatMessage]
Get a prompt from the MCP server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt_name
|
str
|
The name of the prompt to get |
required |
arguments
|
Optional[Dict[str, str]]
|
Optional arguments to pass to the prompt |
None
|
Returns:
| Type | Description |
|---|---|
List[ChatMessage]
|
The prompt as a list of llama-index ChatMessage objects |
Source code in llama_index/tools/mcp/client.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
workflow_as_mcp #
workflow_as_mcp(workflow: Workflow, workflow_name: Optional[str] = None, workflow_description: Optional[str] = None, start_event_model: Optional[BaseModel] = None, **fastmcp_init_kwargs: Any) -> FastMCP
Convert a workflow to an MCP app.
This will convert any Workflow to an MCP app. It will expose the workflow as a tool
within MCP, which will
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workflow
|
Workflow
|
The workflow to convert. |
required |
workflow_name
|
optional
|
The name of the workflow. Defaults to the workflow class name. |
None
|
workflow_description
|
optional
|
The description of the workflow. Defaults to the workflow docstring. |
None
|
start_event_model
|
optional
|
The start event model of the workflow. Can be a |
None
|
**fastmcp_init_kwargs
|
Any
|
Additional keyword arguments to pass to the FastMCP constructor. |
{}
|
Returns:
| Type | Description |
|---|---|
FastMCP
|
The MCP app object. |
Source code in llama_index/tools/mcp/utils.py
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 | |
get_tools_from_mcp_url #
get_tools_from_mcp_url(command_or_url: str, client: Optional[ClientSession] = None, allowed_tools: Optional[List[str]] = None, include_resources: bool = False) -> List[FunctionTool]
Get tools from an MCP server or command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command_or_url
|
str
|
The command to run or the URL to connect to. |
required |
client
|
optional
|
The client to use to connect to the MCP server. |
None
|
allowed_tools
|
optional
|
The tool names to allow from the MCP server. |
None
|
include_resources
|
optional
|
Whether to include resources in the tool list. |
False
|
Source code in llama_index/tools/mcp/utils.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
aget_tools_from_mcp_url
async
#
aget_tools_from_mcp_url(command_or_url: str, client: Optional[ClientSession] = None, allowed_tools: Optional[List[str]] = None, include_resources: bool = False) -> List[FunctionTool]
Get tools from an MCP server or command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command_or_url
|
str
|
The command to run or the URL to connect to. |
required |
client
|
optional
|
The client to use to connect to the MCP server. |
None
|
allowed_tools
|
optional
|
The tool names to allow from the MCP server. |
None
|
include_resources
|
optional
|
Whether to include resources in the tool list. |
False
|
Source code in llama_index/tools/mcp/utils.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
options: members: - McpToolSpec