Index
Client
dataclass
A high-level MCP client for connecting to MCP servers.
Supports in-memory transport for testing (pass a Server or MCPServer instance), Streamable HTTP transport (pass a URL string), or a custom Transport instance.
Example
from mcp.client import Client
from mcp.server.mcpserver import MCPServer
server = MCPServer("test")
@server.tool()
def add(a: int, b: int) -> int:
return a + b
async def main():
async with Client(server) as client:
result = await client.call_tool("add", {"a": 1, "b": 2})
asyncio.run(main())
Source code in src/mcp/client/client.py
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 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 | |
server
instance-attribute
The MCP server to connect to.
If the server is a Server or MCPServer instance, it will be wrapped in an InMemoryTransport.
If the server is a URL string, it will be used as the URL for a streamable_http_client transport.
If the server is a Transport instance, it will be used directly.
raise_exceptions
class-attribute
instance-attribute
raise_exceptions: bool = False
Whether to raise exceptions from the server.
read_timeout_seconds
class-attribute
instance-attribute
read_timeout_seconds: float | None = None
Timeout for read operations.
sampling_callback
class-attribute
instance-attribute
sampling_callback: SamplingFnT | None = None
Callback for handling sampling requests.
list_roots_callback
class-attribute
instance-attribute
list_roots_callback: ListRootsFnT | None = None
Callback for handling list roots requests.
logging_callback
class-attribute
instance-attribute
logging_callback: LoggingFnT | None = None
Callback for handling logging notifications.
message_handler
class-attribute
instance-attribute
message_handler: MessageHandlerFnT | None = None
Callback for handling raw messages.
client_info
class-attribute
instance-attribute
client_info: Implementation | None = None
Client implementation info to send to server.
elicitation_callback
class-attribute
instance-attribute
elicitation_callback: ElicitationFnT | None = None
Callback for handling elicitation requests.
__aenter__
async
__aenter__() -> Client
Enter the async context manager.
Source code in src/mcp/client/client.py
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 | |
__aexit__
async
__aexit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: Any,
) -> None
Exit the async context manager.
Source code in src/mcp/client/client.py
138 139 140 141 142 | |
session
property
session: ClientSession
Get the underlying ClientSession.
This provides access to the full ClientSession API for advanced use cases.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If accessed before entering the context manager. |
initialize_result
property
initialize_result: InitializeResult
The server's InitializeResult.
Contains server_info, capabilities, instructions, and the negotiated protocol_version. Raises RuntimeError if accessed outside the context manager.
send_ping
async
send_ping(
*, meta: RequestParamsMeta | None = None
) -> EmptyResult
Send a ping request to the server.
Source code in src/mcp/client/client.py
169 170 171 | |
send_progress_notification
async
send_progress_notification(
progress_token: str | int,
progress: float,
total: float | None = None,
message: str | None = None,
) -> None
Send a progress notification to the server.
Source code in src/mcp/client/client.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
set_logging_level
async
set_logging_level(
level: LoggingLevel,
*,
meta: RequestParamsMeta | None = None
) -> EmptyResult
Set the logging level on the server.
Source code in src/mcp/client/client.py
188 189 190 | |
list_resources
async
list_resources(
*,
cursor: str | None = None,
meta: RequestParamsMeta | None = None
) -> ListResourcesResult
List available resources from the server.
Source code in src/mcp/client/client.py
192 193 194 195 196 197 198 199 | |
list_resource_templates
async
list_resource_templates(
*,
cursor: str | None = None,
meta: RequestParamsMeta | None = None
) -> ListResourceTemplatesResult
List available resource templates from the server.
Source code in src/mcp/client/client.py
201 202 203 204 205 206 207 208 | |
read_resource
async
read_resource(
uri: str, *, meta: RequestParamsMeta | None = None
) -> ReadResourceResult
Read a resource from the server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
str
|
The URI of the resource to read. |
required |
meta
|
RequestParamsMeta | None
|
Additional metadata for the request. |
None
|
Returns:
| Type | Description |
|---|---|
ReadResourceResult
|
The resource content. |
Source code in src/mcp/client/client.py
210 211 212 213 214 215 216 217 218 219 220 | |
subscribe_resource
async
subscribe_resource(
uri: str, *, meta: RequestParamsMeta | None = None
) -> EmptyResult
Subscribe to resource updates.
Source code in src/mcp/client/client.py
222 223 224 | |
unsubscribe_resource
async
unsubscribe_resource(
uri: str, *, meta: RequestParamsMeta | None = None
) -> EmptyResult
Unsubscribe from resource updates.
Source code in src/mcp/client/client.py
226 227 228 | |
call_tool
async
call_tool(
name: str,
arguments: dict[str, Any] | None = None,
read_timeout_seconds: float | None = None,
progress_callback: ProgressFnT | None = None,
*,
meta: RequestParamsMeta | None = None
) -> CallToolResult
Call a tool on the server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the tool to call |
required |
arguments
|
dict[str, Any] | None
|
Arguments to pass to the tool |
None
|
read_timeout_seconds
|
float | None
|
Timeout for the tool call |
None
|
progress_callback
|
ProgressFnT | None
|
Callback for progress updates |
None
|
meta
|
RequestParamsMeta | None
|
Additional metadata for the request |
None
|
Returns:
| Type | Description |
|---|---|
CallToolResult
|
The tool result. |
Source code in src/mcp/client/client.py
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 | |
list_prompts
async
list_prompts(
*,
cursor: str | None = None,
meta: RequestParamsMeta | None = None
) -> ListPromptsResult
List available prompts from the server.
Source code in src/mcp/client/client.py
259 260 261 262 263 264 265 266 | |
get_prompt
async
get_prompt(
name: str,
arguments: dict[str, str] | None = None,
*,
meta: RequestParamsMeta | None = None
) -> GetPromptResult
Get a prompt from the server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the prompt |
required |
arguments
|
dict[str, str] | None
|
Arguments to pass to the prompt |
None
|
meta
|
RequestParamsMeta | None
|
Additional metadata for the request |
None
|
Returns:
| Type | Description |
|---|---|
GetPromptResult
|
The prompt content. |
Source code in src/mcp/client/client.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
complete
async
complete(
ref: ResourceTemplateReference | PromptReference,
argument: dict[str, str],
context_arguments: dict[str, str] | None = None,
) -> CompleteResult
Get completions for a prompt or resource template argument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref
|
ResourceTemplateReference | PromptReference
|
Reference to the prompt or resource template |
required |
argument
|
dict[str, str]
|
The argument to complete |
required |
context_arguments
|
dict[str, str] | None
|
Additional context arguments |
None
|
Returns:
| Type | Description |
|---|---|
CompleteResult
|
Completion suggestions. |
Source code in src/mcp/client/client.py
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |
list_tools
async
list_tools(
*,
cursor: str | None = None,
meta: RequestParamsMeta | None = None
) -> ListToolsResult
List available tools from the server.
Source code in src/mcp/client/client.py
301 302 303 | |
send_roots_list_changed
async
send_roots_list_changed() -> None
Send a notification that the roots list has changed.
Source code in src/mcp/client/client.py
305 306 307 308 | |
ClientSession
Bases: BaseSession[ClientRequest, ClientNotification, ClientResult, ServerRequest, ServerNotification]
Source code in src/mcp/client/session.py
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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | |
initialize_result
property
initialize_result: InitializeResult | None
The server's InitializeResult. None until initialize() has been called.
Contains server_info, capabilities, instructions, and the negotiated protocol_version.
experimental
property
experimental: ExperimentalClientFeatures
Experimental APIs for tasks and other features.
Warning
These APIs are experimental and may change without notice.
Example
status = await session.experimental.get_task(task_id)
result = await session.experimental.get_task_result(task_id, CallToolResult)
send_ping
async
send_ping(
*, meta: RequestParamsMeta | None = None
) -> EmptyResult
Send a ping request.
Source code in src/mcp/client/session.py
219 220 221 | |
send_progress_notification
async
send_progress_notification(
progress_token: str | int,
progress: float,
total: float | None = None,
message: str | None = None,
*,
meta: RequestParamsMeta | None = None
) -> None
Send a progress notification.
Source code in src/mcp/client/session.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
set_logging_level
async
set_logging_level(
level: LoggingLevel,
*,
meta: RequestParamsMeta | None = None
) -> EmptyResult
Send a logging/setLevel request.
Source code in src/mcp/client/session.py
245 246 247 248 249 250 251 252 253 254 255 | |
list_resources
async
list_resources(
*, params: PaginatedRequestParams | None = None
) -> ListResourcesResult
Send a resources/list request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PaginatedRequestParams | None
|
Full pagination parameters including cursor and any future fields |
None
|
Source code in src/mcp/client/session.py
257 258 259 260 261 262 263 | |
list_resource_templates
async
list_resource_templates(
*, params: PaginatedRequestParams | None = None
) -> ListResourceTemplatesResult
Send a resources/templates/list request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PaginatedRequestParams | None
|
Full pagination parameters including cursor and any future fields |
None
|
Source code in src/mcp/client/session.py
265 266 267 268 269 270 271 272 273 274 275 276 | |
read_resource
async
read_resource(
uri: str, *, meta: RequestParamsMeta | None = None
) -> ReadResourceResult
Send a resources/read request.
Source code in src/mcp/client/session.py
278 279 280 281 282 283 | |
subscribe_resource
async
subscribe_resource(
uri: str, *, meta: RequestParamsMeta | None = None
) -> EmptyResult
Send a resources/subscribe request.
Source code in src/mcp/client/session.py
285 286 287 288 289 290 | |
unsubscribe_resource
async
unsubscribe_resource(
uri: str, *, meta: RequestParamsMeta | None = None
) -> EmptyResult
Send a resources/unsubscribe request.
Source code in src/mcp/client/session.py
292 293 294 295 296 297 | |
call_tool
async
call_tool(
name: str,
arguments: dict[str, Any] | None = None,
read_timeout_seconds: float | None = None,
progress_callback: ProgressFnT | None = None,
*,
meta: RequestParamsMeta | None = None
) -> CallToolResult
Send a tools/call request with optional progress callback support.
Source code in src/mcp/client/session.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
list_prompts
async
list_prompts(
*, params: PaginatedRequestParams | None = None
) -> ListPromptsResult
Send a prompts/list request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PaginatedRequestParams | None
|
Full pagination parameters including cursor and any future fields |
None
|
Source code in src/mcp/client/session.py
350 351 352 353 354 355 356 | |
get_prompt
async
get_prompt(
name: str,
arguments: dict[str, str] | None = None,
*,
meta: RequestParamsMeta | None = None
) -> GetPromptResult
Send a prompts/get request.
Source code in src/mcp/client/session.py
358 359 360 361 362 363 364 365 366 367 368 369 | |
complete
async
complete(
ref: ResourceTemplateReference | PromptReference,
argument: dict[str, str],
context_arguments: dict[str, str] | None = None,
) -> CompleteResult
Send a completion/complete request.
Source code in src/mcp/client/session.py
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | |
list_tools
async
list_tools(
*, params: PaginatedRequestParams | None = None
) -> ListToolsResult
Send a tools/list request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PaginatedRequestParams | None
|
Full pagination parameters including cursor and any future fields |
None
|
Source code in src/mcp/client/session.py
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | |
send_roots_list_changed
async
send_roots_list_changed() -> None
Send a roots/list_changed notification.
Source code in src/mcp/client/session.py
411 412 413 | |
ClientSessionGroup
Client for managing connections to multiple MCP servers.
This class is responsible for encapsulating management of server connections. It aggregates tools, resources, and prompts from all connected servers.
For auxiliary handlers, such as resource subscription, this is delegated to the client and can be accessed via the session.
Example
name_fn = lambda name, server_info: f"{(server_info.name)}_{name}"
async with ClientSessionGroup(component_name_hook=name_fn) as group:
for server_param in server_params:
await group.connect_to_server(server_param)
...
Source code in src/mcp/client/session_group.py
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 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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |
__init__
__init__(
exit_stack: AsyncExitStack | None = None,
component_name_hook: _ComponentNameHook | None = None,
) -> None
Initializes the MCP client.
Source code in src/mcp/client/session_group.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
__aexit__
async
__aexit__(
_exc_type: type[BaseException] | None,
_exc_val: BaseException | None,
_exc_tb: TracebackType | None,
) -> bool | None
Closes session exit stacks and main exit stack upon completion.
Source code in src/mcp/client/session_group.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
prompts
property
Returns the prompts as a dictionary of names to prompts.
resources
property
Returns the resources as a dictionary of names to resources.
call_tool
async
call_tool(
name: str,
arguments: dict[str, Any] | None = None,
read_timeout_seconds: float | None = None,
progress_callback: ProgressFnT | None = None,
*,
meta: RequestParamsMeta | None = None
) -> CallToolResult
Executes a tool given its name and arguments.
Source code in src/mcp/client/session_group.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
disconnect_from_server
async
disconnect_from_server(session: ClientSession) -> None
Disconnects from a single MCP server.
Source code in src/mcp/client/session_group.py
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 | |
connect_with_session
async
connect_with_session(
server_info: Implementation, session: ClientSession
) -> ClientSession
Connects to a single MCP server.
Source code in src/mcp/client/session_group.py
248 249 250 251 252 253 | |
connect_to_server
async
connect_to_server(
server_params: ServerParameters,
session_params: ClientSessionParameters | None = None,
) -> ClientSession
Connects to a single MCP server.
Source code in src/mcp/client/session_group.py
255 256 257 258 259 260 261 262 | |
StdioServerParameters
Bases: BaseModel
Source code in src/mcp/client/stdio.py
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 | |
args
class-attribute
instance-attribute
Command line arguments to pass to the executable.
env
class-attribute
instance-attribute
The environment to use when spawning the process.
If not specified, the result of get_default_environment() will be used.
cwd
class-attribute
instance-attribute
The working directory to use when spawning the process.
encoding
class-attribute
instance-attribute
encoding: str = 'utf-8'
The text encoding used when sending/receiving messages to the server.
Defaults to utf-8.
encoding_error_handler
class-attribute
instance-attribute
encoding_error_handler: Literal[
"strict", "ignore", "replace"
] = "strict"
The text encoding error handler.
See https://docs.python.org/3/library/codecs.html#codec-base-classes for explanations of possible values.
stdio_client
async
stdio_client(
server: StdioServerParameters, errlog: TextIO = stderr
)
Client transport for stdio: this will connect to a server by spawning a process and communicating with it over stdin/stdout.
Source code in src/mcp/client/stdio.py
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 | |
ServerSession
Bases: BaseSession[ServerRequest, ServerNotification, ServerResult, ClientRequest, ClientNotification]
Source code in src/mcp/server/session.py
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 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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 | |
experimental
property
experimental: ExperimentalServerSessionFeatures
Experimental APIs for server→client task operations.
WARNING: These APIs are experimental and may change without notice.
check_client_capability
check_client_capability(
capability: ClientCapabilities,
) -> bool
Check if the client supports a specific capability.
Source code in src/mcp/server/session.py
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 | |
send_log_message
async
send_log_message(
level: LoggingLevel,
data: Any,
logger: str | None = None,
related_request_id: RequestId | None = None,
) -> None
Send a log message notification.
Source code in src/mcp/server/session.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
send_resource_updated
async
Send a resource updated notification.
Source code in src/mcp/server/session.py
226 227 228 229 230 231 232 | |
create_message
async
create_message(
messages: list[SamplingMessage],
*,
max_tokens: int,
system_prompt: str | None = None,
include_context: IncludeContext | None = None,
temperature: float | None = None,
stop_sequences: list[str] | None = None,
metadata: dict[str, Any] | None = None,
model_preferences: ModelPreferences | None = None,
tools: None = None,
tool_choice: ToolChoice | None = None,
related_request_id: RequestId | None = None
) -> CreateMessageResult
create_message(
messages: list[SamplingMessage],
*,
max_tokens: int,
system_prompt: str | None = None,
include_context: IncludeContext | None = None,
temperature: float | None = None,
stop_sequences: list[str] | None = None,
metadata: dict[str, Any] | None = None,
model_preferences: ModelPreferences | None = None,
tools: list[Tool],
tool_choice: ToolChoice | None = None,
related_request_id: RequestId | None = None
) -> CreateMessageResultWithTools
create_message(
messages: list[SamplingMessage],
*,
max_tokens: int,
system_prompt: str | None = None,
include_context: IncludeContext | None = None,
temperature: float | None = None,
stop_sequences: list[str] | None = None,
metadata: dict[str, Any] | None = None,
model_preferences: ModelPreferences | None = None,
tools: list[Tool] | None = None,
tool_choice: ToolChoice | None = None,
related_request_id: RequestId | None = None
) -> CreateMessageResult | CreateMessageResultWithTools
Send a sampling/create_message request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list[SamplingMessage]
|
The conversation messages to send. |
required |
max_tokens
|
int
|
Maximum number of tokens to generate. |
required |
system_prompt
|
str | None
|
Optional system prompt. |
None
|
include_context
|
IncludeContext | None
|
Optional context inclusion setting. Should only be set to "thisServer" or "allServers" if the client has sampling.context capability. |
None
|
temperature
|
float | None
|
Optional sampling temperature. |
None
|
stop_sequences
|
list[str] | None
|
Optional stop sequences. |
None
|
metadata
|
dict[str, Any] | None
|
Optional metadata to pass through to the LLM provider. |
None
|
model_preferences
|
ModelPreferences | None
|
Optional model selection preferences. |
None
|
tools
|
list[Tool] | None
|
Optional list of tools the LLM can use during sampling. Requires client to have sampling.tools capability. |
None
|
tool_choice
|
ToolChoice | None
|
Optional control over tool usage behavior. Requires client to have sampling.tools capability. |
None
|
related_request_id
|
RequestId | None
|
Optional ID of a related request. |
None
|
Returns:
| Type | Description |
|---|---|
CreateMessageResult | CreateMessageResultWithTools
|
The sampling result from the client. |
Raises:
| Type | Description |
|---|---|
MCPError
|
If tools are provided but client doesn't support them. |
ValueError
|
If tool_use or tool_result message structure is invalid. |
StatelessModeNotSupported
|
If called in stateless HTTP mode. |
Source code in src/mcp/server/session.py
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 | |
list_roots
async
list_roots() -> ListRootsResult
Send a roots/list request.
Source code in src/mcp/server/session.py
349 350 351 352 353 354 355 356 | |
elicit
async
elicit(
message: str,
requested_schema: ElicitRequestedSchema,
related_request_id: RequestId | None = None,
) -> ElicitResult
Send a form mode elicitation/create request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
The message to present to the user. |
required |
requested_schema
|
ElicitRequestedSchema
|
Schema defining the expected response structure. |
required |
related_request_id
|
RequestId | None
|
Optional ID of the request that triggered this elicitation. |
None
|
Returns:
| Type | Description |
|---|---|
ElicitResult
|
The client's response. |
Note
This method is deprecated in favor of elicit_form(). It remains for backward compatibility but new code should use elicit_form().
Source code in src/mcp/server/session.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | |
elicit_form
async
elicit_form(
message: str,
requested_schema: ElicitRequestedSchema,
related_request_id: RequestId | None = None,
) -> ElicitResult
Send a form mode elicitation/create request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
The message to present to the user. |
required |
requested_schema
|
ElicitRequestedSchema
|
Schema defining the expected response structure. |
required |
related_request_id
|
RequestId | None
|
Optional ID of the request that triggered this elicitation. |
None
|
Returns:
| Type | Description |
|---|---|
ElicitResult
|
The client's response with form data. |
Raises:
| Type | Description |
|---|---|
StatelessModeNotSupported
|
If called in stateless HTTP mode. |
Source code in src/mcp/server/session.py
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |
elicit_url
async
elicit_url(
message: str,
url: str,
elicitation_id: str,
related_request_id: RequestId | None = None,
) -> ElicitResult
Send a URL mode elicitation/create request.
This directs the user to an external URL for out-of-band interactions like OAuth flows, credential collection, or payment processing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Human-readable explanation of why the interaction is needed. |
required |
url
|
str
|
The URL the user should navigate to. |
required |
elicitation_id
|
str
|
Unique identifier for tracking this elicitation. |
required |
related_request_id
|
RequestId | None
|
Optional ID of the request that triggered this elicitation. |
None
|
Returns:
| Type | Description |
|---|---|
ElicitResult
|
The client's response indicating acceptance, decline, or cancellation. |
Raises:
| Type | Description |
|---|---|
StatelessModeNotSupported
|
If called in stateless HTTP mode. |
Source code in src/mcp/server/session.py
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 | |
send_ping
async
send_ping() -> EmptyResult
Send a ping request.
Source code in src/mcp/server/session.py
450 451 452 453 454 455 | |
send_progress_notification
async
send_progress_notification(
progress_token: str | int,
progress: float,
total: float | None = None,
message: str | None = None,
related_request_id: str | None = None,
) -> None
Send a progress notification.
Source code in src/mcp/server/session.py
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 | |
send_resource_list_changed
async
send_resource_list_changed() -> None
Send a resource list changed notification.
Source code in src/mcp/server/session.py
478 479 480 | |
send_tool_list_changed
async
send_tool_list_changed() -> None
Send a tool list changed notification.
Source code in src/mcp/server/session.py
482 483 484 | |
send_prompt_list_changed
async
send_prompt_list_changed() -> None
Send a prompt list changed notification.
Source code in src/mcp/server/session.py
486 487 488 | |
send_elicit_complete
async
Send an elicitation completion notification.
This should be sent when a URL mode elicitation has been completed out-of-band to inform the client that it may retry any requests that were waiting for this elicitation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
elicitation_id
|
str
|
The unique identifier of the completed elicitation |
required |
related_request_id
|
RequestId | None
|
Optional ID of the request that triggered this notification |
None
|
Source code in src/mcp/server/session.py
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 | |
send_message
async
send_message(message: SessionMessage) -> None
Send a raw session message.
This is primarily used by TaskResultHandler to deliver queued messages (elicitation/sampling requests) to the client during task execution.
WARNING: This is a low-level experimental method that may change without notice. Prefer using higher-level methods like send_notification() or send_request() for normal operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
SessionMessage
|
The session message to send |
required |
Source code in src/mcp/server/session.py
672 673 674 675 676 677 678 679 680 681 682 683 684 685 | |
stdio_server
async
Server transport for stdio: this communicates with an MCP client by reading from the current process' stdin and writing to stdout.
Source code in src/mcp/server/stdio.py
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 | |
MCPError
Bases: Exception
Exception type raised when an error arrives over an MCP connection.
Source code in src/mcp/shared/exceptions.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
UrlElicitationRequiredError
Bases: MCPError
Specialized error for when a tool requires URL mode elicitation(s) before proceeding.
Servers can raise this error from tool handlers to indicate that the client must complete one or more URL elicitations before the request can be processed.
Example
raise UrlElicitationRequiredError([
ElicitRequestURLParams(
message="Authorization required for your files",
url="https://example.com/oauth/authorize",
elicitation_id="auth-001"
)
])
Source code in src/mcp/shared/exceptions.py
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 | |
__init__
__init__(
elicitations: list[ElicitRequestURLParams],
message: str | None = None,
)
Initialize UrlElicitationRequiredError.
Source code in src/mcp/shared/exceptions.py
79 80 81 82 83 84 85 86 87 88 89 90 | |
elicitations
property
elicitations: list[ElicitRequestURLParams]
The list of URL elicitations required before the request can proceed.
from_error
classmethod
from_error(error: ErrorData) -> UrlElicitationRequiredError
Reconstruct from an ErrorData received over the wire.
Source code in src/mcp/shared/exceptions.py
97 98 99 100 101 102 103 104 105 106 | |
CallToolRequest
Bases: Request[CallToolRequestParams, Literal['tools/call']]
Used by the client to invoke a tool provided by the server.
Source code in src/mcp/types/_types.py
1191 1192 1193 1194 1195 | |
ClientCapabilities
Bases: MCPModel
Capabilities a client may support.
Source code in src/mcp/types/_types.py
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | |
experimental
class-attribute
instance-attribute
Experimental, non-standard capabilities that the client supports.
sampling
class-attribute
instance-attribute
sampling: SamplingCapability | None = None
Present if the client supports sampling from an LLM. Can contain fine-grained capabilities like context and tools support.
elicitation
class-attribute
instance-attribute
elicitation: ElicitationCapability | None = None
Present if the client supports elicitation from the user.
roots
class-attribute
instance-attribute
roots: RootsCapability | None = None
Present if the client supports listing roots.
tasks
class-attribute
instance-attribute
tasks: ClientTasksCapability | None = None
Present if the client supports task-augmented requests.
CompleteRequest
Bases: Request[CompleteRequestParams, Literal['completion/complete']]
A request from the client to the server, to ask for completion options.
Source code in src/mcp/types/_types.py
1458 1459 1460 1461 1462 | |
CreateMessageRequest
Bases: Request[CreateMessageRequestParams, Literal['sampling/createMessage']]
A request from the server to sample an LLM via the client.
Source code in src/mcp/types/_types.py
1362 1363 1364 1365 1366 | |
CreateMessageResult
Bases: Result
The client's response to a sampling/createMessage request from the server.
This is the backwards-compatible version that returns single content (no arrays). Used when the request does not include tools.
Source code in src/mcp/types/_types.py
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 | |
role
instance-attribute
role: Role
The role of the message sender (typically 'assistant' for LLM responses).
content
instance-attribute
content: SamplingContent
Response content. Single content block (text, image, or audio).
stop_reason
class-attribute
instance-attribute
stop_reason: StopReason | None = None
The reason why sampling stopped, if known.
CreateMessageResultWithTools
Bases: Result
The client's response to a sampling/createMessage request when tools were provided.
This version supports array content for tool use flows.
Source code in src/mcp/types/_types.py
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 | |
role
instance-attribute
role: Role
The role of the message sender (typically 'assistant' for LLM responses).
content
instance-attribute
content: (
SamplingMessageContentBlock
| list[SamplingMessageContentBlock]
)
Response content. May be a single content block or an array. May include ToolUseContent if stop_reason is 'toolUse'.
stop_reason
class-attribute
instance-attribute
stop_reason: StopReason | None = None
The reason why sampling stopped, if known. 'toolUse' indicates the model wants to use a tool.
content_as_list
property
content_as_list: list[SamplingMessageContentBlock]
Returns the content as a list of content blocks, regardless of whether it was originally a single block or a list.
ErrorData
Bases: BaseModel
Error information for JSON-RPC error responses.
Source code in src/mcp/types/jsonrpc.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
message
instance-attribute
message: str
A short description of the error.
The message SHOULD be limited to a concise single sentence.
data
class-attribute
instance-attribute
data: Any = None
Additional information about the error.
The value of this member is defined by the sender (e.g. detailed error information, nested errors, etc.).
GetPromptRequest
Bases: Request[GetPromptRequestParams, Literal['prompts/get']]
Used by the client to get a prompt provided by the server.
Source code in src/mcp/types/_types.py
876 877 878 879 880 | |
GetPromptResult
Bases: Result
The server's response to a prompts/get request from the client.
Source code in src/mcp/types/_types.py
1065 1066 1067 1068 1069 1070 | |
description
class-attribute
instance-attribute
description: str | None = None
An optional description for the prompt.
Implementation
Bases: BaseMetadata
Describes the name and version of an MCP implementation.
Source code in src/mcp/types/_types.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
title
class-attribute
instance-attribute
title: str | None = None
An optional human-readable title for this implementation.
description
class-attribute
instance-attribute
description: str | None = None
An optional human-readable description of what this implementation does.
website_url
class-attribute
instance-attribute
website_url: str | None = None
An optional URL of the website for this implementation.
InitializedNotification
Bases: Notification[NotificationParams | None, Literal['notifications/initialized']]
This notification is sent from the client to the server after initialization has finished.
Source code in src/mcp/types/_types.py
574 575 576 577 578 579 580 | |
InitializeRequest
Bases: Request[InitializeRequestParams, Literal['initialize']]
This request is sent from the client to the server when it first connects, asking it to begin initialization.
Source code in src/mcp/types/_types.py
554 555 556 557 558 559 560 | |
InitializeResult
Bases: Result
After receiving an initialize request from the client, the server sends this.
Source code in src/mcp/types/_types.py
563 564 565 566 567 568 569 570 571 | |
JSONRPCError
Bases: BaseModel
A response to a request that indicates an error occurred.
Source code in src/mcp/types/jsonrpc.py
74 75 76 77 78 79 | |
JSONRPCRequest
Bases: BaseModel
A JSON-RPC request that expects a response.
Source code in src/mcp/types/jsonrpc.py
13 14 15 16 17 18 19 | |
JSONRPCResponse
Bases: BaseModel
A successful (non-error) response to a request.
Source code in src/mcp/types/jsonrpc.py
31 32 33 34 35 36 | |
ListPromptsRequest
Bases: PaginatedRequest[Literal['prompts/list']]
Sent from the client to request a list of prompts and prompt templates.
Source code in src/mcp/types/_types.py
828 829 830 831 | |
ListPromptsResult
Bases: PaginatedResult
The server's response to a prompts/list request from the client.
Source code in src/mcp/types/_types.py
861 862 863 864 | |
ListResourcesRequest
Bases: PaginatedRequest[Literal['resources/list']]
Sent from the client to request a list of resources the server has.
Source code in src/mcp/types/_types.py
621 622 623 624 | |
ListResourcesResult
Bases: PaginatedResult
The server's response to a resources/list request from the client.
Source code in src/mcp/types/_types.py
689 690 691 692 | |
ListToolsResult
Bases: PaginatedResult
The server's response to a tools/list request from the client.
Source code in src/mcp/types/_types.py
1178 1179 1180 1181 | |
LoggingMessageNotification
Bases: Notification[LoggingMessageNotificationParams, Literal['notifications/message']]
Notification of a log message passed from server to client.
Source code in src/mcp/types/_types.py
1247 1248 1249 1250 1251 | |
Notification
Bases: MCPModel, Generic[NotificationParamsT, MethodT]
Base class for JSON-RPC notifications.
Source code in src/mcp/types/_types.py
116 117 118 119 120 | |
PingRequest
Bases: Request[RequestParams | None, Literal['ping']]
A ping, issued by either the server or the client, to check that the other party is still alive.
Source code in src/mcp/types/_types.py
583 584 585 586 587 588 589 | |
ProgressNotification
Bases: Notification[ProgressNotificationParams, Literal['notifications/progress']]
An out-of-band notification used to inform the receiver of a progress update for a long-running request.
Source code in src/mcp/types/_types.py
614 615 616 617 618 | |
PromptsCapability
Bases: MCPModel
Capability for prompts operations.
Source code in src/mcp/types/_types.py
328 329 330 331 332 | |
list_changed
class-attribute
instance-attribute
list_changed: bool | None = None
Whether this server supports notifications for changes to the prompt list.
ReadResourceRequest
Bases: Request[ReadResourceRequestParams, Literal['resources/read']]
Sent from the client to the server, to read a specific resource URI.
Source code in src/mcp/types/_types.py
717 718 719 720 721 | |
ReadResourceResult
Bases: Result
The server's response to a resources/read request from the client.
Source code in src/mcp/types/_types.py
755 756 757 758 | |
Resource
Bases: BaseMetadata
A known resource that the server is capable of reading.
Source code in src/mcp/types/_types.py
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 | |
description
class-attribute
instance-attribute
description: str | None = None
A description of what this resource represents.
mime_type
class-attribute
instance-attribute
mime_type: str | None = None
The MIME type of this resource, if known.
size
class-attribute
instance-attribute
size: int | None = None
The size of the raw resource content, in bytes (i.e., before base64 encoding or any tokenization), if known.
This can be used by Hosts to display file sizes and estimate context window usage.
icons
class-attribute
instance-attribute
An optional list of icons for this resource.
meta
class-attribute
instance-attribute
meta: Meta | None = Field(alias='_meta', default=None)
See MCP specification for notes on _meta usage.
ResourcesCapability
Bases: MCPModel
Capability for resources operations.
Source code in src/mcp/types/_types.py
335 336 337 338 339 340 341 | |
ResourceUpdatedNotification
Bases: Notification[ResourceUpdatedNotificationParams, Literal['notifications/resources/updated']]
A notification from the server to the client, informing it that a resource has changed and may need to be read again.
Source code in src/mcp/types/_types.py
817 818 819 820 821 822 823 824 825 | |
RootsCapability
Bases: MCPModel
Capability for root operations.
Source code in src/mcp/types/_types.py
202 203 204 205 206 | |
list_changed
class-attribute
instance-attribute
list_changed: bool | None = None
Whether the client supports notifications for changes to the roots list.
SamplingCapability
Bases: MCPModel
Sampling capability structure, allowing fine-grained capability advertisement.
Source code in src/mcp/types/_types.py
246 247 248 249 250 251 252 253 254 255 256 257 258 | |
context
class-attribute
instance-attribute
context: SamplingContextCapability | None = None
Present if the client supports non-'none' values for includeContext parameter. SOFT-DEPRECATED: New implementations should use tools parameter instead.
tools
class-attribute
instance-attribute
tools: SamplingToolsCapability | None = None
Present if the client supports tools and toolChoice parameters in sampling requests. Presence indicates full tool calling support during sampling.
SamplingContent
module-attribute
SamplingContent: TypeAlias = (
TextContent | ImageContent | AudioContent
)
Basic content types for sampling responses (without tool use).
Used for backwards-compatible CreateMessageResult when tools are not used.
SamplingContextCapability
Bases: MCPModel
Capability for context inclusion during sampling.
Indicates support for non-'none' values in the includeContext parameter. SOFT-DEPRECATED: New implementations should use tools parameter instead.
Source code in src/mcp/types/_types.py
209 210 211 212 213 214 | |
SamplingMessage
Bases: MCPModel
Describes a message issued to or received from an LLM API.
Source code in src/mcp/types/_types.py
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 | |
content
instance-attribute
content: (
SamplingMessageContentBlock
| list[SamplingMessageContentBlock]
)
Message content. Can be a single content block or an array of content blocks for multi-modal messages and tool interactions.
meta
class-attribute
instance-attribute
meta: Meta | None = Field(alias='_meta', default=None)
See MCP specification for notes on _meta usage.
content_as_list
property
content_as_list: list[SamplingMessageContentBlock]
Returns the content as a list of content blocks, regardless of whether it was originally a single block or a list.
SamplingMessageContentBlock
module-attribute
SamplingMessageContentBlock: TypeAlias = (
TextContent
| ImageContent
| AudioContent
| ToolUseContent
| ToolResultContent
)
Content block types allowed in sampling messages.
SamplingToolsCapability
Bases: MCPModel
Capability indicating support for tool calling during sampling.
When present in ClientCapabilities.sampling, indicates that the client supports the tools and toolChoice parameters in sampling requests.
Source code in src/mcp/types/_types.py
217 218 219 220 221 222 | |
ServerCapabilities
Bases: MCPModel
Capabilities that a server may support.
Source code in src/mcp/types/_types.py
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | |
experimental
class-attribute
instance-attribute
Experimental, non-standard capabilities that the server supports.
logging
class-attribute
instance-attribute
logging: LoggingCapability | None = None
Present if the server supports sending log messages to the client.
prompts
class-attribute
instance-attribute
prompts: PromptsCapability | None = None
Present if the server offers any prompt templates.
resources
class-attribute
instance-attribute
resources: ResourcesCapability | None = None
Present if the server offers any resources to read.
tools
class-attribute
instance-attribute
tools: ToolsCapability | None = None
Present if the server offers any tools to call.
completions
class-attribute
instance-attribute
completions: CompletionsCapability | None = None
Present if the server offers autocompletion suggestions for prompts and resources.
tasks
class-attribute
instance-attribute
tasks: ServerTasksCapability | None = None
Present if the server supports task-augmented requests.
SetLevelRequest
Bases: Request[SetLevelRequestParams, Literal['logging/setLevel']]
A request from the client to the server, to enable or adjust logging.
Source code in src/mcp/types/_types.py
1226 1227 1228 1229 1230 | |
SubscribeRequest
Bases: Request[SubscribeRequestParams, Literal['resources/subscribe']]
Sent from the client to request resources/updated notifications from the server whenever a particular resource changes.
Source code in src/mcp/types/_types.py
782 783 784 785 786 787 788 | |
Tool
Bases: BaseMetadata
Definition for a tool the client can call.
Source code in src/mcp/types/_types.py
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 | |
description
class-attribute
instance-attribute
description: str | None = None
A human-readable description of the tool.
input_schema
instance-attribute
A JSON Schema object defining the expected parameters for the tool.
output_schema
class-attribute
instance-attribute
An optional JSON Schema object defining the structure of the tool's output returned in the structured_content field of a CallToolResult.
icons
class-attribute
instance-attribute
An optional list of icons for this tool.
annotations
class-attribute
instance-attribute
annotations: ToolAnnotations | None = None
Optional additional tool information.
meta
class-attribute
instance-attribute
meta: Meta | None = Field(alias='_meta', default=None)
See MCP specification for notes on _meta usage.
ToolChoice
Bases: MCPModel
Controls tool usage behavior during sampling.
Allows the server to specify whether and how the LLM should use tools in its response.
Source code in src/mcp/types/_types.py
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 | |
mode
class-attribute
instance-attribute
mode: Literal['auto', 'required', 'none'] | None = None
Controls when tools are used: - "auto": Model decides whether to use tools (default) - "required": Model MUST use at least one tool before completing - "none": Model should not use tools
ToolResultContent
Bases: MCPModel
Content representing the result of a tool execution.
This content type appears in user messages as a response to a ToolUseContent from the assistant. It contains the output of executing the requested tool.
Source code in src/mcp/types/_types.py
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 | |
type
class-attribute
instance-attribute
type: Literal['tool_result'] = 'tool_result'
Discriminator for tool result content.
tool_use_id
instance-attribute
tool_use_id: str
The unique identifier that corresponds to the tool call's id field.
content
class-attribute
instance-attribute
content: list[ContentBlock] = []
A list of content objects representing the tool result. Defaults to empty list if not provided.
structured_content
class-attribute
instance-attribute
Optional structured tool output that matches the tool's outputSchema (if defined).
is_error
class-attribute
instance-attribute
is_error: bool | None = None
Whether the tool execution resulted in an error.
meta
class-attribute
instance-attribute
meta: Meta | None = Field(alias='_meta', default=None)
See MCP specification for notes on _meta usage.
ToolsCapability
Bases: MCPModel
Capability for tools operations.
Source code in src/mcp/types/_types.py
344 345 346 347 348 | |
list_changed
class-attribute
instance-attribute
list_changed: bool | None = None
Whether this server supports notifications for changes to the tool list.
ToolUseContent
Bases: MCPModel
Content representing an assistant's request to invoke a tool.
This content type appears in assistant messages when the LLM wants to call a tool during sampling. The server should execute the tool and return a ToolResultContent in the next user message.
Source code in src/mcp/types/_types.py
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 | |
type
class-attribute
instance-attribute
type: Literal['tool_use'] = 'tool_use'
Discriminator for tool use content.
name
instance-attribute
name: str
The name of the tool to invoke. Must match a tool name from the request's tools array.
id
instance-attribute
id: str
Unique identifier for this tool call, used to correlate with ToolResultContent.
input
instance-attribute
Arguments to pass to the tool. Must conform to the tool's inputSchema.
meta
class-attribute
instance-attribute
meta: Meta | None = Field(alias='_meta', default=None)
See MCP specification for notes on _meta usage.
UnsubscribeRequest
Bases: Request[UnsubscribeRequestParams, Literal['resources/unsubscribe']]
Sent from the client to request cancellation of resources/updated notifications from the server.
Source code in src/mcp/types/_types.py
798 799 800 801 802 803 804 | |