Skip to content
prod e051e98
Browse

MCP

Interoperate

The Model Context Protocol (MCP) is a standard way for agents and tools to talk across applications. Mastra speaks it both directions: as a client it consumes tools exposed by external MCP servers; as a server it exposes your tools and agents to other MCP-aware apps (IDEs, other agents). This is how a Mastra app plugs into the ecosystem instead of living on an island.

graph LR
EXT["External MCP servers<br/>(GitHub, DBs, …)"] -->|"client: consume tools"| M["Mastra app"]
M -->|"server: expose tools / agents"| APPS["Other MCP-aware apps<br/>(IDEs, agents)"]

Configure an MCPClient with the servers you want, fetch their tools, and hand them to an agent like any local tool. The external server’s capabilities become available to the model.

import { MCPClient } from '@mastra/mcp';
import { Agent } from '@mastra/core/agent';
const mcp = new MCPClient({
servers: {
github: { url: new URL('https://example.com/mcp/github') },
},
});
export const devAgent = new Agent({
id: 'dev-agent',
name: 'Dev Agent',
instructions: 'Use the available tools to inspect repositories.',
model: 'openai/gpt-4o',
tools: await mcp.getTools(),
});

Wrap your tools (and agents) in an MCPServer so other MCP-aware clients can discover and call them. Your Mastra app becomes a building block in someone else’s agent.

import { MCPServer } from '@mastra/mcp';
import { weatherTool } from './tools/weather-tool';
export const server = new MCPServer({
name: 'weather-server',
version: '1.0.0',
tools: { weatherTool },
});

Reference: MCP overview · MCPClient · MCPServer

Next: Deployment — take the whole thing to production.