mastra/Core Concepts

MCP

Model Context Protocol for extending agent capabilities through a standardized protocol

mcpprotocolextensionstools

MCP (Model Context Protocol)

MCP enables extending agent capabilities through a standardized protocol.

Topics

MCP Overview

Model Context Protocol (MCP) provides a standardized way to extend AI agent capabilities.

What is MCP?

MCP is an open protocol that enables AI applications to connect to external data sources and tools in a standardized way.

MCP Servers

MCP servers expose:

  • Resources - Data that agents can read
  • Tools - Functions that agents can call
  • Prompts - Templated prompts for specific tasks

Using MCP Servers

import { MCPClient } from '@mastra/mcp';

const mcpClient = new MCPClient({
  serverUrl: 'https://mcp.example.com',
  apiKey: 'your-api-key',
});

await mcpClient.connect();

// Use tools from the MCP server
const result = await mcpClient.callTool('search', {
  query: 'AI developments',
});

Finding MCP Servers

Browse the MCP server registry to find pre-built integrations for common services and data sources.

Publishing an MCP Server

Share your MCP server with the community.

Creating an MCP Server

import { MCPServer } from '@mastra/mcp';

const server = new MCPServer({
  name: 'my-mcp-server',
  version: '1.0.0',
});

// Add resources
server.addResource({
  name: 'docs',
  uri: 'docs://',
  description: 'Documentation',
});

// Add tools
server.addTool({
  name: 'search',
  description: 'Search documentation',
  inputSchema: z.object({
    query: z.string(),
  }),
  execute: async ({ query }) => {
    return searchDocs(query);
  },
});

Publishing

  1. Package your server as an npm module
  2. Publish to npm
  3. Register in the MCP server registry

Server Requirements

  • Implement the MCP specification
  • Provide clear documentation
  • Include authentication if needed
  • Handle errors gracefully