Smart MCP Agent Template

A Mastra template for creating intelligent AI agents that can dynamically discover, install, and use MCP (Model Context Protocol) servers in run time. This template provides a foundation for building agents that can automatically find and integrate external services through the MCP ecosystem.

šŸš€ Features

  • Dynamic MCP Server Discovery: Search the Smithery registry for available MCP servers
  • Automatic Server Installation: Add servers to your agent's configuration dynamically
  • Persistent Configuration: Servers are permanently added to the codebase
  • File System Access: Built-in filesystem MCP server for project file operations
  • Memory Management: LibSQL-based memory storage for agent state
  • Type Safety: Full TypeScript support with Zod schema validation

šŸ—ļø Project Structure

 1src/
 2ā”œā”€ā”€ mastra/
 3│   ā”œā”€ā”€ agents/
 4│   │   └── smart-mcp-agent.ts      # Main AI agent with MCP capabilities
 5│   ā”œā”€ā”€ tools/
 6│   │   ā”œā”€ā”€ add-mcp-server.ts       # Dynamically add MCP servers to config
 7│   │   ā”œā”€ā”€ discover-servers.ts     # Search Smithery registry for servers
 8│   │   └── list-servers.ts         # List currently configured servers
 9│   ā”œā”€ā”€ index.ts                    # Mastra configuration
10│   ā”œā”€ā”€ mcp.ts                      # MCP client configuration
11│   └── types.ts                    # TypeScript type definitions

šŸ› ļø Installation

  1. Clone the template:

     1npx create-mastra@latest template-smart-mcp-agent
     2cd template-smart-mcp-agent
  2. Install dependencies:

    npm install
  3. Set up environment variables:

     1cp .env.example .env
     2# Edit .env with your API keys
  4. Start development server:

    npm run dev

šŸ”§ Configuration

Environment Variables

VariableDescriptionRequired
OPENAI_API_KEYOpenAI API key for agent modelYes
SMITHERY_API_KEYSmithery registry API keyYes

Agent Configuration

 1import { smartMcpAgent } from './src/mastra/agents/smart-mcp-agent';
 2
 3// Agent is configured with:
 4// - OpenAI GPT-4o-mini model
 5// - LibSQL memory storage
 6// - Dynamic MCP server management

šŸ”„ Workflow

The Smart MCP Agent follows this workflow:

  1. Discovery: Use discover-servers to search for MCP servers on Smithery
  2. Installation: Use add-mcp-server to dynamically add servers to the configuration
  3. Verification: Use list-servers to confirm installation
  4. Usage: Access server tools through the agent

Example Usage

 1// Search for web search servers
 2await discoverServersTool.execute({
 3  context: { query: "web search", limit: 5 }
 4});
 5
 6// Add a web search server
 7await addMcpServerTool.execute({
 8  context: {
 9    name: "websearch",
10    serverId: "@Aas-ee/open-websearch",
11    description: "Web search capabilities"
12  }
13});
14
15// List configured servers
16await listServersTool.execute({ context: {} });

šŸ› ļø Tools

Add MCP Server Tool

Dynamically adds MCP servers to the mcp.ts configuration file.

Input Schema:

 1{
 2  name: z.string().min(1),           // Server name (e.g., "websearch")
 3  serverId: z.string().min(1),       // Full server ID (e.g., "@Aas-ee/open-websearch")
 4  description: z.string().optional() // Optional description
 5}

Features:

  • Automatically converts server names to valid JavaScript identifiers
  • Updates the mcp.ts file with proper configuration
  • Triggers automatic server restart to load new servers
  • Provides clear feedback and next steps

Discover Servers Tool

Searches the Smithery registry for available MCP servers.

Input Schema:

 1{
 2  query: z.string(),                 // Search query
 3  limit: z.number().int().min(1).max(20).optional() // Result limit
 4}

Features:

  • Searches Smithery registry with optional API key
  • Returns server details including name, description, and usage stats
  • Graceful fallback if API is unavailable

List Servers Tool

Lists all currently configured MCP servers.

Features:

  • Reads the current mcp.ts configuration
  • Identifies server types (Smithery, filesystem, etc.)
  • Shows server IDs and configuration details
  • Provides clear status information

šŸ”„ Server Management

Adding Servers

When you add a server using add-mcp-server:

  1. Configuration Update: The server is added to src/mastra/mcp.ts
  2. Automatic Restart: Mastra server restarts to load the new server
  3. Persistence: Server configuration persists across restarts
  4. Verification: Use list-servers to confirm successful addition

Server Types

  • Smithery Servers: Remote MCP servers hosted on Smithery
  • Filesystem Server: Local filesystem access for project files
  • Custom Servers: Any MCP-compatible server

Current Configuration

The template comes with these pre-configured servers:

  • textEditor: Filesystem access for project files
  • geeknews: Tech news and updates
  • aidaily: AI daily news and insights

šŸ›”ļø Error Handling

The template includes comprehensive error handling:

  • MCP Connection Failures: Graceful handling of server connection issues
  • File System Errors: Safe file operations with proper error messages
  • API Rate Limiting: Built-in retry logic for external API calls
  • Validation Errors: Zod schema validation with clear error messages

šŸš€ Development

Available Scripts

  • npm run dev - Start development server with hot reload
  • npm run build - Build for production
  • npm run start - Start production server

Development Workflow

  1. Start the dev server: npm run dev
  2. Access the playground: http://localhost:4111
  3. Test agent interactions: Use the playground to interact with the agent
  4. Add new servers: Use the agent to discover and add MCP servers
  5. Monitor logs: Check terminal output for server status and errors

šŸ“ API Reference

Agent

The smartMcpAgent provides:

  • Model: OpenAI GPT-4o-mini
  • Memory: LibSQL-based persistent storage
  • Tools: Dynamic MCP server management tools
  • Instructions: Comprehensive workflow guidance

MCP Client

The MCP client (buildMcp) manages:

  • Server Connections: Automatic connection to configured servers
  • Tool Discovery: Dynamic tool loading from MCP servers
  • Error Recovery: Graceful handling of connection issues
  • Heartbeat Management: Automatic connection maintenance

šŸ¤ Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

šŸ“„ License

ISC License - see LICENSE file for details.

šŸ”— Resources