Skip to content

API Reference parrot.mcp.agent_tools

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.mcp.agent_tools

View source

Agent-method MCP exposure declaration and reification (FEAT-477, Module 1).

Defines the @mcp_tool decorator and MCPToolDeclaration model used to mark a bound agent method as externally callable over MCP, plus the reification machinery — AgentMethodTool (a real AbstractTool wrapper) and build_exposure_set() (the configure-time scan that turns an agent's @mcp_tool-marked methods into a separate, MCP-only collection).

This module is core-only (G9): it must be importable with no extras installed, so it MUST NOT import anything from ai-parrot-server (parrot.mcp.transports, parrot.mcp.oauth_server, etc.).

OQ2 — the single most important invariant in this feature: the exposure set built by :func:build_exposure_set is a plain list, deliberately never registered into the owning agent's ToolManager. Decorating a method changes what MCP clients can call and nothing else — it does not make the method LLM-callable inside its own agent.

Class AgentMethodTool

An agent method reified as a real AbstractTool.

Built by :func:build_exposure_set from a @mcp_tool-marked method. name, description and args_schema come from the method's MCPToolDeclaration; _execute() invokes the bound method on the owning agent. The agent is held by weak reference only so this tool never drags the agent into tool-serialization paths and never creates a reference cycle (spec §7 Risks).

The agent is resolved per call, never cached as a bound method, so a reloaded agent (BotManager.reload_agent()) is picked up transparently by any mount holding this tool (spec OQ5).

Parameter Type Default Description
agent Any The owning agent instance. Held by weak reference only.
method_name str Name of the async method on agent to invoke.
declaration MCPToolDeclaration The MCPToolDeclaration attached by @mcp_tool.

Class MCPToolDeclaration

Declaration metadata attached by @mcp_tool.

All fields except the hint/limit flags are mandatory — there is no schema inference in v1 (spec §1 Non-Goals). Registration must fail loudly if any mandatory field is missing.

Fields:

Field Type Default Description
name str MCP tool name exposed to clients.
description str Human-readable tool description surfaced to MCP clients.
args_schema type[BaseModel] Pydantic model describing the tool's call arguments.
returns type[BaseModel] Pydantic model describing the tool's return payload.
scope str PBAC action/resource scope enforced when the tool is invoked.
read_only_hint bool False Maps to the MCP readOnlyHint annotation.
idempotent_hint bool False Maps to the MCP idempotentHint annotation.
requires_confirmation bool False Maps to routing_meta["requires_confirmation"], which MCPToolAdapter already honors (destructiveHint precedent).
max_result_tokens int | None None Per-tool result-size cap; None falls back to the mount default.

Attribute MCP_TOOL_ATTR

Function build_exposure_set

Scan agent for @mcp_tool-marked methods and build its exposure set.

Walks the agent's class for coroutine methods carrying an MCPToolDeclaration (attached via MCP_TOOL_ATTR), validates there are no name collisions — neither among the decorated methods themselves nor against tools already registered in agent.tool_manager — and reifies each into an AgentMethodTool.

The returned exposure set is a plain list. It is never registered into agent.tool_manager (OQ2) — callers (the MCP mount, TASK-2602) are responsible for what they do with it.

build_exposure_set(agent: Any) -> list[AgentMethodTool]

Parameter Type Default Description
agent Any The agent instance to scan. Must expose tool_manager if it has any @mcp_tool-decorated method (used for the collision check); agents with none are never asked for it.

Returns: list[AgentMethodTool] — The agent's exposure set — one AgentMethodTool per decorated method. Empty if the agent declares none.

Raises:

  • ValueError — If two decorated methods declare the same MCP tool name, or a decorated name collides with an existing agent.tool_manager tool. The message names the agent class and the offending method(s).

Function mcp_tool

Mark a bound agent method as externally callable over MCP.

Marks only. Reification into an AgentMethodTool happens at configure() time (TASK-2600). The decorated method is NEVER registered into the owning agent's ToolManager and does not become LLM-callable inside that agent (spec OQ2 — the single most important invariant of this feature).

mcp_tool(*, name: str, description: str, args_schema: type[BaseModel], returns: type[BaseModel], scope: str, read_only_hint: bool = False, idempotent_hint: bool = False, requires_confirmation: bool = False, max_result_tokens: int | None = None) -> Callable[[F], F]

Parameter Type Default Description
name str MCP tool name exposed to clients.
description str Human-readable tool description.
args_schema type[BaseModel] Pydantic model describing the call arguments. Mandatory — no schema inference in v1.
returns type[BaseModel] Pydantic model describing the return payload. Mandatory.
scope str PBAC action/resource scope enforced at call time.
read_only_hint bool False MCP readOnlyHint annotation. Defaults to False.
idempotent_hint bool False MCP idempotentHint annotation. Defaults to False.
requires_confirmation bool False Whether MCP callers must pass confirm=true; maps to routing_meta["requires_confirmation"] / destructiveHint. Defaults to False.
max_result_tokens int | None None Per-tool result-size cap overriding the mount default. Defaults to None.

Returns: Callable[[F], F] — A decorator that attaches an MCPToolDeclaration to the decorated async method and returns it unchanged.

Raises:

  • TypeError — If the decorated callable is not an async def method, if a mandatory field is missing/empty, or if args_schema / returns is not a BaseModel subclass.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally