Tools & MCP¶
Declarative tools and the lower-level MCP HTTP server infrastructure. See the ToolKit & tools and MCP servers guides for usage.
ToolKit¶
ToolKit
¶
Base class for declarative MCP tool servers.
Subclass, decorate methods with @tool, call as_server()
to get an MCPServerHandle.
as_server
¶
as_server(server_id: str | None = None) -> MCPServerHandle
Build and return an MCPServerHandle with all @tool methods registered.
Source code in caw/toolkit.py
@tool¶
tool
¶
tool(name: str | None = None, *, description: str | None = None, title: str | None = None, annotations: Any | None = None, structured_output: bool | None = None)
Mark a method as an MCP tool. Does NOT modify the function itself.
Source code in caw/toolkit.py
MCP tool servers¶
MCPServerHandle
dataclass
¶
MCPServerHandle(server_id: str, server: FastMCP, host: str = '127.0.0.1', port: int | None = None, path: str | None = None, _state_instance: Any = None, _server_task: Task | None = None, _uvicorn_server: Server | None = None, uvicorn_log_level: str | None = 'error', _bound_socket: socket | None = None, _daemon_thread: Thread | None = None, _daemon_loop: AbstractEventLoop | None = None, _ready_event: Event | None = None, _startup_error: BaseException | None = None)
Convenience wrapper exposing runner + agent config for a FastMCP server.
runner
¶
Return a sync function that blocks while serving the MCP HTTP endpoint.
Source code in caw/mcp.py
run_in_background
async
¶
Async context manager that runs the HTTP server on a background task.
get_state
¶
start
async
¶
Start the HTTP server in the background with retry on port conflict.
Source code in caw/mcp.py
stop
async
¶
Stop the background HTTP server.
Source code in caw/mcp.py
start_sync
¶
Start the server from a synchronous context.
Spawns a daemon thread with its own event loop, starts the server inside it, and blocks the calling thread until the server is ready.
Source code in caw/mcp.py
stop_sync
¶
Stop the server from a synchronous context.
Source code in caw/mcp.py
mcp_tool
¶
mcp_tool(name: str | None = None, *, title: str | None = None, description: str | None = None, annotations: Any | None = None, icons: list[Any] | None = None, meta: dict[str, Any] | None = None, structured_output: bool | None = None)
Decorator to attach MCP metadata to a tool function.
Source code in caw/mcp.py
register_tool
¶
Register a decorated tool function with a FastMCP server.
Source code in caw/mcp.py
create_mcp_http_server_bundle
¶
create_mcp_http_server_bundle(server_id: str, *, display_name: str, state_factory: Callable[[], Any] | None = None, state_instance: Any = None, state_display_name: str | None = None, state_shutdown: Callable[[Any], None | Awaitable[None]] | None = None, uvicorn_log_level: str | None = 'error', state_logger: Callable[[str], None] | None = None, **fastmcp_kwargs: Any) -> MCPServerHandle
Create a FastMCP server plus helpers for running it over HTTP.
Source code in caw/mcp.py
create_stateless_tool_server
¶
create_stateless_tool_server(funcs: list[Callable[..., Any]], *, server_id: str | None = None, display_name: str = 'stateless_tools') -> MCPServerHandle
Bundle plain functions into a single MCPServerHandle.
Each function is registered as an MCP tool. Functions may optionally be
decorated with mcp_tool or tool to supply
metadata; bare functions are registered using their name and docstring.
Source code in caw/mcp.py
create_subagent_tool_server
¶
create_subagent_tool_server(spec: Any, traj_dir: str, jsonl_path: str | None = None) -> MCPServerHandle
Create an HTTP tool server that exposes a subagent as a callable tool.
Parameters¶
spec
An AgentSpec with name, description, system_prompt, model.
traj_dir
Directory where subagent trajectory JSON files are written.
jsonl_path
Path to the parent session's JSONL log (for interleaved subagent events).