What is a Tool?
Essentially, a Tool in Universal MCP is a Python function (or a method within an Application class) that has been packaged with metadata to make it discoverable and usable by AI agents. TheTool class (defined in tools/tools.py) encapsulates:
fn: The actual callable Python function that performs the work.name: A unique name for the tool.description: A human-readable summary of what the tool does (often parsed from the function’s docstring). This is crucial for the AI to understand when to use the tool.args_description: Descriptions for each argument the function accepts.parameters: A JSON schema defining the expected input arguments for the tool. This allows for structured input and validation.fn_metadata: An instance ofFuncMetadatawhich handles function signature analysis, argument validation, and pre-parsing of JSON arguments.is_async: A boolean indicating if the tool’s function is asynchronous.
How Tools are Defined
Tools are typically defined in one of two ways:-
As methods within an Application class: This is the most common approach. Each public method in your
APIApplicationorGraphQLApplicationthat you want to expose as a tool for the AI will be automatically converted into aToolobject when registered.In this example,get_user_detailswould become a tool. -
As standalone Python functions: You can also define regular Python functions and register them as tools with a
ToolManager.This is useful for creating utility tools or functions that don’t naturally belong to a specific external API Application. (Ref:examples/langraph.py-calculate function)
Key Features of Universal MCP Tools
- Automatic Metadata Generation: The system automatically parses docstrings (summary, args, returns, tags) and type hints to generate the
descriptionandparametersschema for the tool. (Ref:tools/tools.py - Tool.from_function,utils/docstring_parser.py) - Input Validation: Uses Pydantic models (
ArgModelBase,FuncMetadata) for robust validation of arguments passed to tools. - JSON Pre-parsing:
FuncMetadatacan pre-parse stringified JSON arguments, which is often helpful when interacting with LLMs that might not format JSON perfectly. - Synchronous and Asynchronous Support: Tools can be either regular Python functions or
asyncfunctions. TheToolManagerandToolexecution logic handle both seamlessly. - Adaptability: Tools can be converted into formats compatible with different AI agent frameworks like Langchain and OpenAI Functions using adapters. (Ref:
tools/adapters.py)