Overview
This example demonstrates how to build a custom MCP (Model Context Protocol) server using FastMCP and integrate it with AI agents. The example implements an email service that can configure SMTP settings and send emails through MCP tools.Features
- Custom MCP Server - Built with FastMCP framework
- Tool Definition - Expose functions as MCP tools
- Client Integration - Connect agents to custom server
- Stdio Communication - Standard input/output protocol
- Type Safety - Proper parameter typing and validation
- Error Handling - Robust error management
Architecture
Prerequisites
- Python 3.8+
- FastMCP library
- OpenAI Agents SDK
- SMTP credentials (Gmail App Password)
- Nebius API key
Installation
Building the MCP Server
Server Implementation
Createmcp-server.py:
Key Components
FastMCP Initialization
FastMCP Initialization
Tool Definition
Tool Definition
@mcp.tool() decorator exposes functions as MCP tools. The function’s docstring and type hints are used for tool metadata.State Management
State Management
Stdio Transport
Stdio Transport
Building the MCP Client
Client Implementation
Createmcp-client.py:
Client Components
MCPServerStdio Configuration
MCPServerStdio Configuration
Agent Configuration
Agent Configuration
Running the Agent
Running the Agent
Usage
Running the Server Standalone
Running with Client
- Start the MCP server as subprocess
- Create an agent connected to the server
- Execute the email workflow
- Display results
Advanced Patterns
Multiple Tools
Add multiple tools to your server:Resources
Expose data resources:Prompts
Provide reusable prompts:Context Management
Use FastMCP context for dependency injection:Error Handling
Robust error handling:Testing
Unit Testing Tools
Integration Testing
Deployment
Local Development
Production
Docker Example
Best Practices
Type Safety
- Use type hints for all parameters
- Return consistent data structures
- Validate input before processing
- Document return value schemas
Error Handling
- Return errors as data, not exceptions
- Provide clear error messages
- Include error codes when appropriate
- Log errors for debugging
Documentation
- Write clear docstrings for tools
- Document required vs optional params
- Provide usage examples
- Explain side effects
State Management
- Keep state minimal and clear
- Use configuration tools for setup
- Provide state inspection tools
- Handle concurrent access
Troubleshooting
Server Not Starting
Server Not Starting
Check the command and args:
Tools Not Discovered
Tools Not Discovered
Ensure:
- Server is running with
transport='stdio' - Tools are decorated with
@mcp.tool() - Client has
cache_tools_list=True
Agent Can't Call Tools
Agent Can't Call Tools
Check:
mcp_servers=[mcp_server]in Agent config- Server subprocess is still running
- No stdio communication errors
Source Code
View the complete implementation at:~/workspace/source/mcp_ai_agents/custom_mcp_server/
FastMCP
FastMCP framework documentation
OpenAI Agents SDK
OpenAI Agents SDK
Related Examples
Database MCP Agent
GibsonAI database management
Couchbase MCP Server
Couchbase MCP integration