MCP Utils API¶
hatchling.mcp_utils
¶
All the MCP utilities for Hatchling. Including Client-Server communication, tool execution, and event handling.
Classes¶
MCPClient
¶
Client for MCP servers that manages connections and tool execution.
Source code in hatchling/mcp_utils/client.py
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 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 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 | |
Functions¶
__init__(settings=None, python_executable_resolver=None)
¶
Initialize the MCP client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
AppSettings
|
The application settings instance. If None, uses the singleton instance. |
None
|
python_executable_resolver
|
Callable[[], str]
|
Function to resolve the Python executable for the current environment. If None, defaults to "python". |
None
|
Source code in hatchling/mcp_utils/client.py
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 42 43 44 45 46 47 48 49 50 51 52 53 | |
connect(server_path)
async
¶
Connect to an MCP server via stdio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_path
|
str
|
Path to the server script (.py file). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if connection was successful, False otherwise. |
Source code in hatchling/mcp_utils/client.py
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 | |
disconnect()
async
¶
Disconnect from the MCP server and clean up resources.
Source code in hatchling/mcp_utils/client.py
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 | |
execute_tool(tool_name, arguments)
async
¶
Execute an MCP tool by name with given arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool to execute. |
required |
arguments
|
Dict[str, Any
|
Arguments to pass to the tool. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Result of the tool execution. |
Raises:
| Type | Description |
|---|---|
ConnectionError
|
If not connected to MCP server. |
ValueError
|
If the tool is not found. |
TimeoutError
|
If the tool execution times out. |
Exception
|
For any other errors during execution. |
Source code in hatchling/mcp_utils/client.py
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 | |
get_citations()
async
¶
Get citations from the MCP server.
Returns:
| Type | Description |
|---|---|
Dict[str, str]
|
Dict[str, str]: Dictionary with origin and MCP citations. |
Source code in hatchling/mcp_utils/client.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
MCPManager
¶
Centralized manager for everything MCP-related: servers, clients, and adapters.
Source code in hatchling/mcp_utils/manager.py
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 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 | |
Attributes¶
is_connected
property
¶
Check if the manager has any active connections to MCP servers.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if connected to at least one MCP server, False otherwise. |
publisher
property
¶
Access to the EventPublisher for MCP lifecycle events.
Returns:
| Name | Type | Description |
|---|---|---|
EventPublisher |
EventPublisher
|
The publisher for MCP-related events. |
Functions¶
__init__(settings=None)
¶
Initialize the MCP manager if not already initialized.
Source code in hatchling/mcp_utils/manager.py
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 | |
__new__()
¶
Ensure singleton pattern implementation.
Returns:
| Name | Type | Description |
|---|---|---|
MCPManager |
The singleton instance of the MCPManager. |
Source code in hatchling/mcp_utils/manager.py
26 27 28 29 30 31 32 33 34 35 | |
connect_to_servers(server_paths=None)
async
¶
Connect to all configured MCP servers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_paths
|
Optional[List[str]]
|
List of paths to MCP server scripts. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
False if not valid server paths are provided |
Source code in hatchling/mcp_utils/manager.py
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 | |
disable_tool(tool_name)
¶
Disable a specific tool if it exists and is enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool to disable. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the tool was disabled, False if it was already disabled or doesn't exist. |
Source code in hatchling/mcp_utils/manager.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 411 | |
disconnect_all()
async
¶
Disconnect from all MCP servers.
Source code in hatchling/mcp_utils/manager.py
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 | |
enable_tool(tool_name)
¶
Enable a specific tool if it exists and is disabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool to enable. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the tool was enabled, False if it was already enabled or doesn't exist. |
Source code in hatchling/mcp_utils/manager.py
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 | |
execute_tool(tool_name, arguments)
async
¶
Execute a tool by name with the given arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool to execute. |
required |
arguments
|
Dict[str, Any]
|
Arguments to pass to the tool. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Result of the tool execution. |
Raises:
| Type | Description |
|---|---|
ConnectionError
|
If not connected to any MCP server. |
ValueError
|
If the tool is not found in any connected MCP server. |
Source code in hatchling/mcp_utils/manager.py
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 | |
get_all_managed_tools()
¶
Get all managed tools (both enabled and disabled).
Returns:
| Type | Description |
|---|---|
Dict[str, MCPToolInfo]
|
Dict[str, MCPToolInfo]: Dictionary mapping tool names to all MCPToolInfo objects. |
Source code in hatchling/mcp_utils/manager.py
334 335 336 337 338 339 340 | |
get_citations_for_session()
async
¶
Get citations for all servers used in the current session.
Returns:
| Type | Description |
|---|---|
Dict[str, Dict[str, str]]
|
Dict[str, Dict[str, str]]: Dictionary of citations for each server. |
Source code in hatchling/mcp_utils/manager.py
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 | |
get_enabled_tools()
¶
Get all enabled tools with their information.
Returns:
| Type | Description |
|---|---|
Dict[str, MCPToolInfo]
|
Dict[str, MCPToolInfo]: Dictionary mapping tool names to enabled MCPToolInfo objects. |
Source code in hatchling/mcp_utils/manager.py
323 324 325 326 327 328 329 330 331 332 | |
get_tool_status(tool_name)
¶
Get the current status and information for a tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool to get status for. |
required |
Returns:
| Type | Description |
|---|---|
Optional[MCPToolInfo]
|
Optional[MCPToolInfo]: Tool information if found, None otherwise. |
Source code in hatchling/mcp_utils/manager.py
413 414 415 416 417 418 419 420 421 422 | |
reset_session_tracking()
¶
Reset the tracking of which servers were used.
Source code in hatchling/mcp_utils/manager.py
475 476 477 | |
validate_server_paths(server_paths)
¶
Validate server paths and return the list of valid absolute paths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_paths
|
List[str]
|
List of server paths to validate. |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: List of valid absolute paths. |
Source code in hatchling/mcp_utils/manager.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
MCPServerAPI
¶
Clean API for MCP server management and debugging.
This class provides a simplified interface for: - Server connection management - Tool discovery and management - Manual tool execution for debugging - Server health monitoring
Source code in hatchling/mcp_utils/mcp_server_api.py
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 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 | |
Functions¶
connect_servers(server_paths=None)
async
staticmethod
¶
Connect to MCP servers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_paths
|
List[str]
|
List of paths to MCP server scripts. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if at least one server connected successfully. |
Source code in hatchling/mcp_utils/mcp_server_api.py
64 65 66 67 68 69 70 71 72 73 74 75 | |
disable_tool(tool_name)
staticmethod
¶
Disable a specific tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool to disable. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if tool was disabled successfully. |
Source code in hatchling/mcp_utils/mcp_server_api.py
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 | |
disconnect_all_servers()
async
staticmethod
¶
Disconnect from all MCP servers.
Source code in hatchling/mcp_utils/mcp_server_api.py
77 78 79 80 | |
enable_tool(tool_name)
staticmethod
¶
Enable a specific tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool to enable. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if tool was enabled successfully. |
Source code in hatchling/mcp_utils/mcp_server_api.py
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 | |
execute_tool_manually(tool_name, arguments)
async
staticmethod
¶
Execute an MCP tool manually for debugging purposes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool to execute. |
required |
arguments
|
Dict[str, Any]
|
Arguments to pass to the tool. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[bool, Any, Optional[str]]
|
Tuple[bool, Any, Optional[str]]: Success flag, result, and error message if any. |
Source code in hatchling/mcp_utils/mcp_server_api.py
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |
get_all_tools()
staticmethod
¶
Get list of all available MCP tools.
Returns:
| Type | Description |
|---|---|
List[MCPToolSummary]
|
List[MCPToolSummary]: List of tool summaries. |
Source code in hatchling/mcp_utils/mcp_server_api.py
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 | |
get_enabled_tools()
staticmethod
¶
Get list of enabled MCP tools.
Returns:
| Type | Description |
|---|---|
List[MCPToolSummary]
|
List[MCPToolSummary]: List of enabled tool summaries. |
Source code in hatchling/mcp_utils/mcp_server_api.py
175 176 177 178 179 180 181 182 | |
get_health_summary()
staticmethod
¶
Get overall health summary of MCP system.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: Health summary including server and tool counts. |
Source code in hatchling/mcp_utils/mcp_server_api.py
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 | |
get_server_list()
staticmethod
¶
Get list of all configured MCP servers with their status.
Returns:
| Type | Description |
|---|---|
List[MCPServerInfo]
|
List[MCPServerInfo]: List of server information. |
Source code in hatchling/mcp_utils/mcp_server_api.py
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 | |
get_server_status(server_path)
staticmethod
¶
Get detailed status for a specific server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_path
|
str
|
Path to the MCP server script. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
MCPServerInfo |
MCPServerInfo
|
Server status information. |
Source code in hatchling/mcp_utils/mcp_server_api.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 | |
get_session_citations()
async
staticmethod
¶
Get citations for all servers used in the current session.
Returns:
| Type | Description |
|---|---|
Dict[str, Dict[str, str]]
|
Dict[str, Dict[str, str]]: Server citations. |
Source code in hatchling/mcp_utils/mcp_server_api.py
376 377 378 379 380 381 382 383 | |
get_tool_info(tool_name)
staticmethod
¶
Get detailed information about a specific tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool. |
required |
Returns:
| Type | Description |
|---|---|
Optional[MCPToolSummary]
|
Optional[MCPToolSummary]: Tool information if found. |
Source code in hatchling/mcp_utils/mcp_server_api.py
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 | |
get_tool_schema(tool_name)
staticmethod
¶
Get the JSON schema for a tool's arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
Optional[Dict[str, Any]]: Tool schema if available. |
Source code in hatchling/mcp_utils/mcp_server_api.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | |
get_tools_by_server(server_path)
staticmethod
¶
Get all tools provided by a specific server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_path
|
str
|
Path to the MCP server script. |
required |
Returns:
| Type | Description |
|---|---|
List[MCPToolSummary]
|
List[MCPToolSummary]: List of tools from the server. |
Source code in hatchling/mcp_utils/mcp_server_api.py
184 185 186 187 188 189 190 191 192 193 194 | |
reset_session_tracking()
staticmethod
¶
Reset session tracking for citations.
Source code in hatchling/mcp_utils/mcp_server_api.py
385 386 387 388 | |
MCPToolInfo
dataclass
¶
Information about an MCP tool in the lifecycle management system.
Source code in hatchling/mcp_utils/mcp_tool_data.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |
MCPToolStatus
¶
Bases: Enum
Status of an MCP tool in the lifecycle management system.
Source code in hatchling/mcp_utils/mcp_tool_data.py
6 7 8 9 10 | |
MCPToolStatusReason
¶
Bases: Enum
Reasons why an MCP tool has a particular status.
Source code in hatchling/mcp_utils/mcp_tool_data.py
13 14 15 16 17 18 19 20 21 22 23 24 25 | |
Core MCP Components¶
hatchling.mcp_utils.client
¶
Classes¶
MCPClient
¶
Client for MCP servers that manages connections and tool execution.
Source code in hatchling/mcp_utils/client.py
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 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 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 | |
Functions¶
__init__(settings=None, python_executable_resolver=None)
¶
Initialize the MCP client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
AppSettings
|
The application settings instance. If None, uses the singleton instance. |
None
|
python_executable_resolver
|
Callable[[], str]
|
Function to resolve the Python executable for the current environment. If None, defaults to "python". |
None
|
Source code in hatchling/mcp_utils/client.py
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 42 43 44 45 46 47 48 49 50 51 52 53 | |
connect(server_path)
async
¶
Connect to an MCP server via stdio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_path
|
str
|
Path to the server script (.py file). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if connection was successful, False otherwise. |
Source code in hatchling/mcp_utils/client.py
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 | |
disconnect()
async
¶
Disconnect from the MCP server and clean up resources.
Source code in hatchling/mcp_utils/client.py
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 | |
execute_tool(tool_name, arguments)
async
¶
Execute an MCP tool by name with given arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool to execute. |
required |
arguments
|
Dict[str, Any
|
Arguments to pass to the tool. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Result of the tool execution. |
Raises:
| Type | Description |
|---|---|
ConnectionError
|
If not connected to MCP server. |
ValueError
|
If the tool is not found. |
TimeoutError
|
If the tool execution times out. |
Exception
|
For any other errors during execution. |
Source code in hatchling/mcp_utils/client.py
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 | |
get_citations()
async
¶
Get citations from the MCP server.
Returns:
| Type | Description |
|---|---|
Dict[str, str]
|
Dict[str, str]: Dictionary with origin and MCP citations. |
Source code in hatchling/mcp_utils/client.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
hatchling.mcp_utils.manager
¶
Classes¶
MCPManager
¶
Centralized manager for everything MCP-related: servers, clients, and adapters.
Source code in hatchling/mcp_utils/manager.py
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 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 | |
Attributes¶
is_connected
property
¶
Check if the manager has any active connections to MCP servers.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if connected to at least one MCP server, False otherwise. |
publisher
property
¶
Access to the EventPublisher for MCP lifecycle events.
Returns:
| Name | Type | Description |
|---|---|---|
EventPublisher |
EventPublisher
|
The publisher for MCP-related events. |
Functions¶
__init__(settings=None)
¶
Initialize the MCP manager if not already initialized.
Source code in hatchling/mcp_utils/manager.py
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 | |
__new__()
¶
Ensure singleton pattern implementation.
Returns:
| Name | Type | Description |
|---|---|---|
MCPManager |
The singleton instance of the MCPManager. |
Source code in hatchling/mcp_utils/manager.py
26 27 28 29 30 31 32 33 34 35 | |
connect_to_servers(server_paths=None)
async
¶
Connect to all configured MCP servers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_paths
|
Optional[List[str]]
|
List of paths to MCP server scripts. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
False if not valid server paths are provided |
Source code in hatchling/mcp_utils/manager.py
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 | |
disable_tool(tool_name)
¶
Disable a specific tool if it exists and is enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool to disable. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the tool was disabled, False if it was already disabled or doesn't exist. |
Source code in hatchling/mcp_utils/manager.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 411 | |
disconnect_all()
async
¶
Disconnect from all MCP servers.
Source code in hatchling/mcp_utils/manager.py
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 | |
enable_tool(tool_name)
¶
Enable a specific tool if it exists and is disabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool to enable. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the tool was enabled, False if it was already enabled or doesn't exist. |
Source code in hatchling/mcp_utils/manager.py
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 | |
execute_tool(tool_name, arguments)
async
¶
Execute a tool by name with the given arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool to execute. |
required |
arguments
|
Dict[str, Any]
|
Arguments to pass to the tool. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Result of the tool execution. |
Raises:
| Type | Description |
|---|---|
ConnectionError
|
If not connected to any MCP server. |
ValueError
|
If the tool is not found in any connected MCP server. |
Source code in hatchling/mcp_utils/manager.py
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 | |
get_all_managed_tools()
¶
Get all managed tools (both enabled and disabled).
Returns:
| Type | Description |
|---|---|
Dict[str, MCPToolInfo]
|
Dict[str, MCPToolInfo]: Dictionary mapping tool names to all MCPToolInfo objects. |
Source code in hatchling/mcp_utils/manager.py
334 335 336 337 338 339 340 | |
get_citations_for_session()
async
¶
Get citations for all servers used in the current session.
Returns:
| Type | Description |
|---|---|
Dict[str, Dict[str, str]]
|
Dict[str, Dict[str, str]]: Dictionary of citations for each server. |
Source code in hatchling/mcp_utils/manager.py
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 | |
get_enabled_tools()
¶
Get all enabled tools with their information.
Returns:
| Type | Description |
|---|---|
Dict[str, MCPToolInfo]
|
Dict[str, MCPToolInfo]: Dictionary mapping tool names to enabled MCPToolInfo objects. |
Source code in hatchling/mcp_utils/manager.py
323 324 325 326 327 328 329 330 331 332 | |
get_tool_status(tool_name)
¶
Get the current status and information for a tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool to get status for. |
required |
Returns:
| Type | Description |
|---|---|
Optional[MCPToolInfo]
|
Optional[MCPToolInfo]: Tool information if found, None otherwise. |
Source code in hatchling/mcp_utils/manager.py
413 414 415 416 417 418 419 420 421 422 | |
reset_session_tracking()
¶
Reset the tracking of which servers were used.
Source code in hatchling/mcp_utils/manager.py
475 476 477 | |
validate_server_paths(server_paths)
¶
Validate server paths and return the list of valid absolute paths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_paths
|
List[str]
|
List of server paths to validate. |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: List of valid absolute paths. |
Source code in hatchling/mcp_utils/manager.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
hatchling.mcp_utils.mcp_server_api
¶
MCP Server Management API.
This module provides a clean, command-friendly API for managing MCP servers, tools, and debugging operations. It wraps the MCPManager functionality with a simplified interface suitable for CLI commands and user interaction.
Classes¶
MCPServerAPI
¶
Clean API for MCP server management and debugging.
This class provides a simplified interface for: - Server connection management - Tool discovery and management - Manual tool execution for debugging - Server health monitoring
Source code in hatchling/mcp_utils/mcp_server_api.py
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 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 | |
Functions¶
connect_servers(server_paths=None)
async
staticmethod
¶
Connect to MCP servers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_paths
|
List[str]
|
List of paths to MCP server scripts. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if at least one server connected successfully. |
Source code in hatchling/mcp_utils/mcp_server_api.py
64 65 66 67 68 69 70 71 72 73 74 75 | |
disable_tool(tool_name)
staticmethod
¶
Disable a specific tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool to disable. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if tool was disabled successfully. |
Source code in hatchling/mcp_utils/mcp_server_api.py
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 | |
disconnect_all_servers()
async
staticmethod
¶
Disconnect from all MCP servers.
Source code in hatchling/mcp_utils/mcp_server_api.py
77 78 79 80 | |
enable_tool(tool_name)
staticmethod
¶
Enable a specific tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool to enable. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if tool was enabled successfully. |
Source code in hatchling/mcp_utils/mcp_server_api.py
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 | |
execute_tool_manually(tool_name, arguments)
async
staticmethod
¶
Execute an MCP tool manually for debugging purposes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool to execute. |
required |
arguments
|
Dict[str, Any]
|
Arguments to pass to the tool. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[bool, Any, Optional[str]]
|
Tuple[bool, Any, Optional[str]]: Success flag, result, and error message if any. |
Source code in hatchling/mcp_utils/mcp_server_api.py
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |
get_all_tools()
staticmethod
¶
Get list of all available MCP tools.
Returns:
| Type | Description |
|---|---|
List[MCPToolSummary]
|
List[MCPToolSummary]: List of tool summaries. |
Source code in hatchling/mcp_utils/mcp_server_api.py
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 | |
get_enabled_tools()
staticmethod
¶
Get list of enabled MCP tools.
Returns:
| Type | Description |
|---|---|
List[MCPToolSummary]
|
List[MCPToolSummary]: List of enabled tool summaries. |
Source code in hatchling/mcp_utils/mcp_server_api.py
175 176 177 178 179 180 181 182 | |
get_health_summary()
staticmethod
¶
Get overall health summary of MCP system.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: Health summary including server and tool counts. |
Source code in hatchling/mcp_utils/mcp_server_api.py
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 | |
get_server_list()
staticmethod
¶
Get list of all configured MCP servers with their status.
Returns:
| Type | Description |
|---|---|
List[MCPServerInfo]
|
List[MCPServerInfo]: List of server information. |
Source code in hatchling/mcp_utils/mcp_server_api.py
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 | |
get_server_status(server_path)
staticmethod
¶
Get detailed status for a specific server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_path
|
str
|
Path to the MCP server script. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
MCPServerInfo |
MCPServerInfo
|
Server status information. |
Source code in hatchling/mcp_utils/mcp_server_api.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 | |
get_session_citations()
async
staticmethod
¶
Get citations for all servers used in the current session.
Returns:
| Type | Description |
|---|---|
Dict[str, Dict[str, str]]
|
Dict[str, Dict[str, str]]: Server citations. |
Source code in hatchling/mcp_utils/mcp_server_api.py
376 377 378 379 380 381 382 383 | |
get_tool_info(tool_name)
staticmethod
¶
Get detailed information about a specific tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool. |
required |
Returns:
| Type | Description |
|---|---|
Optional[MCPToolSummary]
|
Optional[MCPToolSummary]: Tool information if found. |
Source code in hatchling/mcp_utils/mcp_server_api.py
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 | |
get_tool_schema(tool_name)
staticmethod
¶
Get the JSON schema for a tool's arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
Optional[Dict[str, Any]]: Tool schema if available. |
Source code in hatchling/mcp_utils/mcp_server_api.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | |
get_tools_by_server(server_path)
staticmethod
¶
Get all tools provided by a specific server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server_path
|
str
|
Path to the MCP server script. |
required |
Returns:
| Type | Description |
|---|---|
List[MCPToolSummary]
|
List[MCPToolSummary]: List of tools from the server. |
Source code in hatchling/mcp_utils/mcp_server_api.py
184 185 186 187 188 189 190 191 192 193 194 | |
reset_session_tracking()
staticmethod
¶
Reset session tracking for citations.
Source code in hatchling/mcp_utils/mcp_server_api.py
385 386 387 388 | |
MCPServerInfo
dataclass
¶
Information about an MCP server.
Source code in hatchling/mcp_utils/mcp_server_api.py
27 28 29 30 31 32 33 34 35 | |
MCPServerStatus
¶
Bases: Enum
Status of an MCP server connection.
Source code in hatchling/mcp_utils/mcp_server_api.py
19 20 21 22 23 24 | |
MCPToolSummary
dataclass
¶
Summary information about an MCP tool.
Source code in hatchling/mcp_utils/mcp_server_api.py
38 39 40 41 42 43 44 45 46 | |
Tool Management¶
hatchling.mcp_utils.mcp_tool_call_subscriber
¶
MCP Tool Call Subscriber for handling LLM_TOOL_CALL_REQUEST events from LLM streams.
This module provides a subscriber that listens for LLM_TOOL_CALL_REQUEST events from LLM providers and dispatches them to MCPToolExecution for processing.
Classes¶
MCPToolCallSubscriber
¶
Bases: EventSubscriber
Subscriber that handles LLM_TOOL_CALL_REQUEST events and dispatches them for execution.
This subscriber only processes one tool call per request ID, using a rolling buffer to avoid memory growth and ensure out-of-order tool calls are ignored.
Source code in hatchling/mcp_utils/mcp_tool_call_subscriber.py
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 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 | |
Functions¶
__init__(tool_execution)
¶
Initialize the MCP tool call subscriber.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_execution
|
MCPToolExecution
|
The tool execution manager to dispatch calls to. |
required |
Source code in hatchling/mcp_utils/mcp_tool_call_subscriber.py
24 25 26 27 28 29 30 31 32 | |
get_subscribed_events()
¶
Get the list of event types this subscriber handles.
Returns:
| Type | Description |
|---|---|
|
List[EventType]: List of event types this subscriber handles. |
Source code in hatchling/mcp_utils/mcp_tool_call_subscriber.py
34 35 36 37 38 39 40 | |
on_event(event)
¶
Handle incoming events.
Only the first tool call for each request ID is processed. Subsequent tool calls for the same request ID are ignored. The buffer ensures only the last two request IDs are tracked, preventing memory growth.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
Event
|
The event to handle. |
required |
Source code in hatchling/mcp_utils/mcp_tool_call_subscriber.py
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 | |
hatchling.mcp_utils.mcp_tool_data
¶
Classes¶
MCPToolInfo
dataclass
¶
Information about an MCP tool in the lifecycle management system.
Source code in hatchling/mcp_utils/mcp_tool_data.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |
MCPToolStatus
¶
Bases: Enum
Status of an MCP tool in the lifecycle management system.
Source code in hatchling/mcp_utils/mcp_tool_data.py
6 7 8 9 10 | |
MCPToolStatusReason
¶
Bases: Enum
Reasons why an MCP tool has a particular status.
Source code in hatchling/mcp_utils/mcp_tool_data.py
13 14 15 16 17 18 19 20 21 22 23 24 25 | |
hatchling.mcp_utils.mcp_tool_execution
¶
MCP tool execution management with event publishing.
This module provides enhanced functionality for handling tool execution requests from LLMs, managing tool calling chains, and processing tool results with event-driven architecture.
Classes¶
MCPToolExecution
¶
Manages tool execution and tool calling chains with event publishing.
Source code in hatchling/mcp_utils/mcp_tool_execution.py
18 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 | |
Attributes¶
event_publisher
property
¶
Get the stream publisher for this tool execution manager.
Returns:
| Name | Type | Description |
|---|---|---|
EventPublisher |
EventPublisher
|
The stream publisher instance. |
Functions¶
__init__(settings=None)
¶
Initialize the MCP tool execution manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
AppSettings
|
The application settings. If None, uses the singleton instance. |
None
|
Source code in hatchling/mcp_utils/mcp_tool_execution.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
execute_tool(parsed_tool_call)
async
¶
Execute a tool and return its result.
Sends the tool call to the MCPManager for execution and publishes events
for tool call dispatched, progress, result, and error handling.
You can subscribe to event_publisher of this class to receive
MCP_TOOL_CALL_DISPATCHED, MCP_TOOL_CALL_PROGRESS, MCP_TOOL_CALL_RESULT, and MCP_TOOL_CALL_ERROR events.
That will allow you to react to tool calls in real-time and handle them accordingly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parsed_tool_call
|
ToolCallParsedResult
|
The parsed tool call containing tool_id, function_name, and arguments. |
required |
Source code in hatchling/mcp_utils/mcp_tool_execution.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 | |
execute_tool_sync(parsed_tool_call)
¶
Synchronous wrapper for execute_tool that handles async execution internally.
This method creates a task to execute the tool asynchronously without blocking the caller. It's designed for use in synchronous contexts where you want to dispatch tool execution but don't need to wait for the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parsed_tool_call
|
ToolCallParsedResult
|
The parsed tool call containing tool_id, function_name, and arguments. |
required |
Source code in hatchling/mcp_utils/mcp_tool_execution.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
reset_for_new_query(query)
¶
Reset tool execution state for a new user query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The user's query that's starting a new conversation. |
required |
Source code in hatchling/mcp_utils/mcp_tool_execution.py
49 50 51 52 53 54 55 56 57 | |
hatchling.mcp_utils.mcp_tool_lifecycle_subscriber
¶
Classes¶
ToolLifecycleSubscriber
¶
Bases: EventSubscriber
Subscriber that manages MCP tool lifecycle and maintains tool cache in the format required by the LLM provider.
This subscriber listens for MCP server and tool lifecycle events and maintains a cache of all discovered tools with their current status. It provides methods to get enabled tools for use in LLM payloads.
Source code in hatchling/mcp_utils/mcp_tool_lifecycle_subscriber.py
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 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 | |
Functions¶
__init__(provider_name, mcp_to_provider_tool_func)
¶
Initialize the tool lifecycle subscriber.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider_name
|
str
|
Name of the LLM provider using this subscriber. |
required |
mcp_to_provider_tool_func
|
Callable
|
Function to convert MCP tools to provider-specific format. |
required |
Source code in hatchling/mcp_utils/mcp_tool_lifecycle_subscriber.py
18 19 20 21 22 23 24 25 26 27 28 | |
clear_cache()
¶
Clear the tool cache.
Source code in hatchling/mcp_utils/mcp_tool_lifecycle_subscriber.py
185 186 187 188 | |
get_all_tools()
¶
Get all tools (enabled and disabled).
Returns:
| Type | Description |
|---|---|
Dict[str, MCPToolInfo]
|
Dict[str, MCPToolInfo]: Dictionary mapping tool names to all tool info. |
Source code in hatchling/mcp_utils/mcp_tool_lifecycle_subscriber.py
165 166 167 168 169 170 171 | |
get_enabled_tools()
¶
Get all currently enabled tools.
Returns:
| Type | Description |
|---|---|
Dict[str, MCPToolInfo]
|
Dict[str, MCPToolInfo]: Dictionary mapping tool names to enabled tool info. |
Source code in hatchling/mcp_utils/mcp_tool_lifecycle_subscriber.py
154 155 156 157 158 159 160 161 162 163 | |
get_subscribed_events()
¶
Return MCP lifecycle event types this subscriber is interested in.
Returns:
| Type | Description |
|---|---|
List[EventType]
|
List[EventType]: MCP lifecycle event types. |
Source code in hatchling/mcp_utils/mcp_tool_lifecycle_subscriber.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
get_tool_count()
¶
Get count of enabled and disabled tools.
Returns:
| Type | Description |
|---|---|
Dict[str, int]
|
Dict[str, int]: Dictionary with 'enabled' and 'disabled' counts. |
Source code in hatchling/mcp_utils/mcp_tool_lifecycle_subscriber.py
173 174 175 176 177 178 179 180 181 182 183 | |
on_event(event)
¶
Handle MCP lifecycle events and update tool cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
Event
|
The event to handle. |
required |
Source code in hatchling/mcp_utils/mcp_tool_lifecycle_subscriber.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
prettied_reason(reason)
¶
Get a prettified string representation of the tool status reason.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reason
|
MCPToolStatusReason
|
The reason to prettify. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Prettified reason string. |
Source code in hatchling/mcp_utils/mcp_tool_lifecycle_subscriber.py
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 | |