DocsReferenceModel Context Protocol (MCP)MCP Client

MastraMCPClient

The MastraMCPClient class provides a client implementation for interacting with Model Context Protocol (MCP) servers. It handles connection management, resource discovery, and tool execution through the MCP protocol.

Constructor

Creates a new instance of the MastraMCPClient.

constructor({
    name,
    version = '1.0.0',
    server,
    capabilities = {},
}: {
    name: string;
    server: StdioServerParameters;
    capabilities?: ClientCapabilities;
    version?: string;
})

Parameters

name:

string
The name identifier for this client instance.

version?:

string
The version of the client.

server:

StdioServerParameters
Configuration parameters for the stdio server connection.

capabilities?:

ClientCapabilities
Optional capabilities configuration for the client.

Methods

connect()

Establishes a connection with the MCP server.

async connect(): Promise<void>

disconnect()

Closes the connection with the MCP server.

async disconnect(): Promise<void>

resources()

Retrieves the list of available resources from the server.

async resources(): Promise<ListResourcesResult>

tools()

Fetches and initializes available tools from the server, converting them into Mastra-compatible tool formats.

async tools(): Promise<Record<string, Tool>>

Returns an object mapping tool names to their corresponding Mastra tool implementations.

Examples

Using with Mastra Agent

import { Agent } from '@mastra/core';
import { MastraMCPClient } from '@mastra/mcp-client';
 
// Initialize the EverArt MCP client
const everArtClient = new MastraMCPClient({
    name: 'everart',
    server: {
        command: '/usr/local/bin/docker',
        args: ['run', '-i', '--rm', '--network=host', '-e', 'EVERART_API_KEY', 'mcp/everart'],
        env: {
            EVERART_API_KEY: process.env.EVERART_API_KEY!,
        },
    },
});
 
// Create a Mastra Agent
const agent = new Agent({
    name: 'everart',
    instructions: 'You are my artist. Include the url in your response.',
    model: {
        provider: 'ANTHROPIC',
        name: 'claude-3-5-sonnet-20241022',
        toolChoice: 'auto',
    },
});
 
// Example usage in an async function
async function main() {
    try {
        // Connect to the MCP server
        await everArtClient.connect();
        
        // Get available tools
        const tools = await everArtClient.tools();
        
        // Use the agent with the MCP tools
        const response = await agent.generate('Can you make me a picture of a dog?', {
            toolsets: {
                everart: tools,
            },
        });
        
        console.log(response.text);
        
    } catch (error) {
        console.error('Error:', error);
    } finally {
        // Always disconnect when done
        await everArtClient.disconnect();
    }
}

MIT 2025 © Nextra.