Base
Tool execution node for PyAgenity graph workflows.
This module provides the ToolNode class, which serves as a unified registry and executor for callable functions from various sources including local functions, MCP (Model Context Protocol) tools, Composio adapters, and LangChain tools. The ToolNode is designed with a modular architecture using mixins to handle different tool providers.
The ToolNode maintains compatibility with PyAgenity's dependency injection system and publishes execution events for monitoring and debugging purposes.
Typical usage example
def my_tool(query: str) -> str:
return f"Result for: {query}"
tools = ToolNode([my_tool])
result = await tools.invoke("my_tool", {"query": "test"}, "call_id", config, state)
Classes:
Name | Description |
---|---|
ToolNode |
A unified registry and executor for callable functions from various tool providers. |
Attributes:
Name | Type | Description |
---|---|---|
logger |
|
Attributes¶
Classes¶
ToolNode
¶
Bases: SchemaMixin
, LocalExecMixin
, MCPMixin
, ComposioMixin
, LangChainMixin
, KwargsResolverMixin
A unified registry and executor for callable functions from various tool providers.
ToolNode serves as the central hub for managing and executing tools from multiple sources: - Local Python functions - MCP (Model Context Protocol) tools - Composio adapter tools - LangChain tools
The class uses a mixin-based architecture to separate concerns and maintain clean integration with different tool providers. It provides both synchronous and asynchronous execution methods with comprehensive event publishing and error handling.
Attributes:
Name | Type | Description |
---|---|---|
_funcs |
dict[str, Callable]
|
Dictionary mapping function names to callable functions. |
_client |
Client | None
|
Optional MCP client for remote tool execution. |
_composio |
ComposioAdapter | None
|
Optional Composio adapter for external integrations. |
_langchain |
Any | None
|
Optional LangChain adapter for LangChain tools. |
mcp_tools |
list[str]
|
List of available MCP tool names. |
composio_tools |
list[str]
|
List of available Composio tool names. |
langchain_tools |
list[str]
|
List of available LangChain tool names. |
Example
# Define local tools
def weather_tool(location: str) -> str:
return f"Weather in {location}: Sunny, 25°C"
def calculator(a: int, b: int) -> int:
return a + b
# Create ToolNode with local functions
tools = ToolNode([weather_tool, calculator])
# Execute a tool
result = await tools.invoke(
name="weather_tool",
args={"location": "New York"},
tool_call_id="call_123",
config={"user_id": "user1"},
state=agent_state,
)
Methods:
Name | Description |
---|---|
__init__ |
Initialize ToolNode with functions and optional tool adapters. |
all_tools |
Get all available tools from all configured providers. |
all_tools_sync |
Synchronously get all available tools from all configured providers. |
get_local_tool |
Generate OpenAI-compatible tool definitions for all registered local functions. |
invoke |
Execute a specific tool by name with the provided arguments. |
stream |
Execute a tool with streaming support, yielding incremental results. |
Source code in pyagenity/graph/tool_node/base.py
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 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 |
|
Attributes¶
Functions¶
__init__
¶
__init__(functions, client=None, composio_adapter=None, langchain_adapter=None)
Initialize ToolNode with functions and optional tool adapters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Iterable[Callable]
|
Iterable of callable functions to register as tools. Each function
will be registered with its |
required |
|
Client | None
|
Optional MCP (Model Context Protocol) client for remote tool access. Requires 'fastmcp' and 'mcp' packages to be installed. |
None
|
|
ComposioAdapter | None
|
Optional Composio adapter for external integrations and third-party API access. |
None
|
|
Any | None
|
Optional LangChain adapter for accessing LangChain tools and integrations. |
None
|
Raises:
Type | Description |
---|---|
ImportError
|
If MCP client is provided but required packages are not installed. |
TypeError
|
If any item in functions is not callable. |
Note
When using MCP client functionality, ensure you have installed the required
dependencies with: pip install pyagenity[mcp]
Source code in pyagenity/graph/tool_node/base.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 |
|
all_tools
async
¶
all_tools()
Get all available tools from all configured providers.
Retrieves and combines tool definitions from local functions, MCP client, Composio adapter, and LangChain adapter. Each tool definition includes the function schema with parameters and descriptions.
Returns:
Type | Description |
---|---|
list[dict]
|
List of tool definitions in OpenAI function calling format. Each dict |
list[dict]
|
contains 'type': 'function' and 'function' with name, description, |
list[dict]
|
and parameters schema. |
Example
tools = await tool_node.all_tools()
# Returns:
# [
# {
# "type": "function",
# "function": {
# "name": "weather_tool",
# "description": "Get weather information for a location",
# "parameters": {
# "type": "object",
# "properties": {
# "location": {"type": "string"}
# },
# "required": ["location"]
# }
# }
# }
# ]
Source code in pyagenity/graph/tool_node/base.py
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 |
|
all_tools_sync
¶
all_tools_sync()
Synchronously get all available tools from all configured providers.
This is a synchronous wrapper around the async all_tools() method. It uses asyncio.run() to handle async operations from MCP, Composio, and LangChain adapters.
Returns:
Type | Description |
---|---|
list[dict]
|
List of tool definitions in OpenAI function calling format. |
Note
Prefer using the async all_tools()
method when possible, especially
in async contexts, to avoid potential event loop issues.
Source code in pyagenity/graph/tool_node/base.py
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 |
|
get_local_tool
¶
get_local_tool()
Generate OpenAI-compatible tool definitions for all registered local functions.
Inspects all registered functions in _funcs and automatically generates tool schemas by analyzing function signatures, type annotations, and docstrings. Excludes injectable parameters that are provided by the framework.
Returns:
Type | Description |
---|---|
list[dict]
|
List of tool definitions in OpenAI function calling format. Each |
list[dict]
|
definition includes the function name, description (from docstring), |
list[dict]
|
and complete parameter schema with types and required fields. |
Example
For a function:
def calculate(a: int, b: int, operation: str = "add") -> int:
'''Perform arithmetic calculation.'''
return a + b if operation == "add" else a - b
Returns:
[
{
"type": "function",
"function": {
"name": "calculate",
"description": "Perform arithmetic calculation.",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "integer"},
"b": {"type": "integer"},
"operation": {"type": "string", "default": "add"},
},
"required": ["a", "b"],
},
},
}
]
Note
Parameters listed in INJECTABLE_PARAMS (like 'state', 'config', 'tool_call_id') are automatically excluded from the generated schema as they are provided by the framework during execution.
Source code in pyagenity/graph/tool_node/schema.py
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 |
|
invoke
async
¶
invoke(name, args, tool_call_id, config, state, callback_manager=Inject[CallbackManager])
Execute a specific tool by name with the provided arguments.
This method handles tool execution across all configured providers (local, MCP, Composio, LangChain) with comprehensive error handling, event publishing, and callback management.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The name of the tool to execute. |
required |
|
dict
|
Dictionary of arguments to pass to the tool function. |
required |
|
str
|
Unique identifier for this tool execution, used for tracking and result correlation. |
required |
|
dict[str, Any]
|
Configuration dictionary containing execution context and user-specific settings. |
required |
|
AgentState
|
Current agent state for context-aware tool execution. |
required |
|
CallbackManager
|
Manager for executing pre/post execution callbacks. Injected via dependency injection if not provided. |
Inject[CallbackManager]
|
Returns:
Type | Description |
---|---|
Any
|
Message object containing tool execution results, either successful |
Any
|
output or error information with appropriate status indicators. |
Example
result = await tool_node.invoke(
name="weather_tool",
args={"location": "Paris", "units": "metric"},
tool_call_id="call_abc123",
config={"user_id": "user1", "session_id": "session1"},
state=current_agent_state,
)
# result is a Message with tool execution results
print(result.content) # Tool output or error information
Note
The method publishes execution events throughout the process for monitoring and debugging purposes. Tool execution is routed based on tool provider precedence: MCP → Composio → LangChain → Local.
Source code in pyagenity/graph/tool_node/base.py
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 |
|
stream
async
¶
stream(name, args, tool_call_id, config, state, callback_manager=Inject[CallbackManager])
Execute a tool with streaming support, yielding incremental results.
Similar to invoke() but designed for tools that can provide streaming responses or when you want to process results as they become available. Currently, most tool providers return complete results, so this method typically yields a single Message with the full result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The name of the tool to execute. |
required |
|
dict
|
Dictionary of arguments to pass to the tool function. |
required |
|
str
|
Unique identifier for this tool execution. |
required |
|
dict[str, Any]
|
Configuration dictionary containing execution context. |
required |
|
AgentState
|
Current agent state for context-aware tool execution. |
required |
|
CallbackManager
|
Manager for executing pre/post execution callbacks. |
Inject[CallbackManager]
|
Yields:
Type | Description |
---|---|
AsyncIterator[Message]
|
Message objects containing tool execution results or status updates. |
AsyncIterator[Message]
|
For most tools, this will yield a single complete result Message. |
Example
async for message in tool_node.stream(
name="data_processor",
args={"dataset": "large_data.csv"},
tool_call_id="call_stream123",
config={"user_id": "user1"},
state=current_state,
):
print(f"Received: {message.content}")
# Process each streamed result
Note
The streaming interface is designed for future expansion where tools may provide true streaming responses. Currently, it provides a consistent async iterator interface over tool results.
Source code in pyagenity/graph/tool_node/base.py
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 |
|