AguiAdapter converts MCP tools into the AG-UI protocol format so a remote agent can discover and invoke them. McpMiddleware (created via createMcpMiddleware) intercepts tool calls from that remote agent and executes them server-side against your MCP servers — bridging the gap when the agent itself runs in a separate Python process (LangGraph, AutoGen, CopilotKit, etc.).
Installation
- npm
- pnpm
- yarn
Imports
AguiAdapter
AguiAdapter takes an MCPClient or MultiSessionClient and produces two tool representations:
getTools()— returnsAguiTool[], each with ahandlerfor server-side execution.getToolDefinitions()— returnsAguiToolDefinition[], JSON Schema definitions without handlers, suitable for sending to a remote agent.
new AguiAdapter(client, options?)
| Parameter | Type | Description |
|---|---|---|
client | MCPClient | MultiSessionClient | The MCP client to source tools from. |
options | AguiAdapterOptions | Optional configuration (see below). |
AguiAdapterOptions
| Field | Type | Default | Description |
|---|---|---|---|
prefix | string | Server ID (8 chars) | Namespace prefix added to every tool name. |
toolRouter | ToolRouter | — | Use a ToolRouter for intelligent tool filtering. |
adapter.getTools()
Returns Promise<AguiTool[]>. Each AguiTool includes:
adapter.getToolDefinitions()
Returns Promise<AguiToolDefinition[]>. Same shape as AguiTool but without handler — safe to serialize and send to a remote agent.
Schema cleaning
The adapter automatically strips JSON Schema properties that Pydantic’s strict validation rejects ($schema, $id, $comment, $defs, prefill, examples, enumTitles, enumDescriptions, and others). This makes the tool definitions compatible with Google ADK, LangGraph, and other Python frameworks that use Pydantic for schema validation.
McpMiddleware / createMcpMiddleware
McpMiddleware is an AG-UI Middleware subclass. It injects MCP tool schemas into each agent run and intercepts TOOL_CALL_START, TOOL_CALL_ARGS, TOOL_CALL_END, and RUN_FINISHED events. When the agent signals it wants to call an MCP tool, the middleware executes it locally against your MCP server and feeds the result back as a TOOL_CALL_RESULT event, then triggers a continuation run so the agent can incorporate the result.
createMcpMiddleware(config)
Factory function that creates and returns an AG-UI-compatible middleware function.
McpMiddlewareConfig
| Field | Type | Description |
|---|---|---|
tools | AguiTool[] | Pre-loaded tools with handlers, typically from adapter.getTools(). |
Event flow
| Event | What the middleware does |
|---|---|
TOOL_CALL_START | Records the tool name and call ID; marks MCP tools as pending. |
TOOL_CALL_ARGS | Accumulates streamed argument deltas into a buffer. |
TOOL_CALL_END | Marks the call as complete (args are fully buffered). |
RUN_FINISHED | Executes all pending MCP tools, emits TOOL_CALL_RESULT events, then triggers a continuation run with the results appended to message history. |
TOOL_CALL_RESULT | Emitted by the middleware with the serialized MCP tool output. |
Complete Next.js example
The following example shows a complete Next.js App Router API route that connects to a remote Python agent (running LangGraph) and executes MCP tools server-side.Connect to your MCP servers
Create a
MultiSessionClient and call connect() inside the route handler. Each request gets its own client scoped to the authenticated user.Get tools with AguiAdapter
Call
adapter.getTools() to obtain AguiTool[] — each tool has a handler that the middleware will call when the remote agent requests it.Attach middleware to the remote agent
Create an
HttpAgent pointing at your Python backend, then attach the MCP middleware. The middleware injects tool schemas into every run and intercepts tool calls.When to use each export
AguiAdapter.getTools()
Use when your Next.js (or other Node.js) server needs to execute MCP tools on behalf of a remote agent. The
handler functions run server-side and have access to authenticated MCP sessions.AguiAdapter.getToolDefinitions()
Use when you need to send tool schemas to a remote agent without handlers — for example, to pre-populate a client-side agent with available tool names and descriptions.
Use
McpMiddleware (via createMcpMiddleware) whenever the AI agent runs on a separate backend that cannot access your MCP sessions directly. The middleware handles the full round-trip: it intercepts the tool call, executes it against your MCP server, and feeds the result back into the agent’s context so it can continue reasoning.Tool naming
Tool names follow the patterntool_<prefix>_<toolName>. The prefix defaults to the first eight characters of the server ID with hyphens removed.
prefix in AguiAdapterOptions to control the namespace:
Disconnected clients
Ifclient.isConnected() returns false when getTools() or getToolDefinitions() is called, the adapter returns an empty array []. The middleware will have no tools to inject and will pass runs through to the remote agent unmodified.