Skip to content

Model Context Protocol (MCP) Servers Cheat Sheet

Overview

Model Context Protocol (MCP) is a universal, open standard designed to connect AI systems with external data sources and tools. MCP servers act as the bridge between AI models (clients) and the external world, enabling AI assistants to access and invoke functions, retrieve information, and interact with various services in a standardized way.

What sets MCP apart is its ability to replace fragmented integrations with a single, unified protocol. Before MCP, each AI model provider had their own proprietary methods for tool use and function calling, creating a complex ecosystem that required developers to implement different integration approaches for each model. MCP solves this by providing a standardized interface that works across different AI models and services, significantly simplifying the development and deployment of AI applications.

MCP servers have emerged as a critical component in the AI infrastructure stack, allowing organizations to build secure, scalable, and standardized connections between their AI models and the tools, data sources, and services they need to access. Whether deployed on cloud platforms or on-premises, MCP servers enable AI systems to safely and efficiently interact with the external world while maintaining control over these interactions.

Core Concepts

Model Context Protocol (MCP)

MCP is the standardized protocol that defines how AI models interact with external tools and services. It provides a universal interface for connecting AI systems with data sources and functions.

MCP Server

An MCP server implements the Model Context Protocol and acts as a bridge between AI models (clients) and external tools or services. It handles requests from AI models, executes the appropriate functions, and returns results.

MCP Client

An MCP client is any AI model or application that communicates with an MCP server to access external tools and services. Clients send requests to the server and receive responses according to the MCP specification.

Tools

Tools are functions or services that an MCP server makes available to AI models. These can include data retrieval functions, computational tools, API integrations, or any other capability that extends the AI model's functionality.

Context

Context refers to the information and capabilities available to an AI model through the MCP server. This includes the tools the model can access, the data it can retrieve, and the operations it can perform.

Installation and Setup

AWS Serverless MCP Server

bash
# Clone the AWS Serverless MCP Server repository
git clone https://github.com/aws-samples/aws-serverless-mcp-server.git
cd aws-serverless-mcp-server

# Install dependencies
npm install

# Deploy using AWS CDK
npm run cdk bootstrap
npm run cdk deploy

Basic Node.js MCP Server

javascript
// Install dependencies
// npm install express cors body-parser

const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');

const app = express();
app.use(cors());
app.use(bodyParser.json());

// Define available tools
const tools = {
  get_weather: async (params) => {
    const { location } = params;
    // Implement weather retrieval logic
    return { temperature: 25, conditions: "Sunny", location };
  },
  search_database: async (params) => {
    const { query } = params;
    // Implement database search logic
    return { results: [`Result for: ${query}`] };
  }
};

// MCP server endpoint
app.post('/mcp', async (req, res) => {
  try {
    const { tool, parameters } = req.body;
    
    if (!tools[tool]) {
      return res.status(400).json({ error: `Tool '${tool}' not found` });
    }
    
    const result = await tools[tool](parameters);
    res.json({ result });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// Start server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`MCP Server running on port ${PORT}`);
});

Python MCP Server

python
# Install dependencies
# pip install fastapi uvicorn pydantic

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Dict, Any, Optional
import uvicorn

app = FastAPI()

# Define request model
class MCPRequest(BaseModel):
    tool: str
    parameters: Dict[str, Any]

# Define available tools
async def get_weather(location: str) -> Dict[str, Any]:
    # Implement weather retrieval logic
    return {"temperature": 25, "conditions": "Sunny", "location": location}

async def search_database(query: str) -> Dict[str, Any]:
    # Implement database search logic
    return {"results": [f"Result for: {query}"]}

# Tool registry
tools = {
    "get_weather": get_weather,
    "search_database": search_database
}

@app.post("/mcp")
async def mcp_endpoint(request: MCPRequest):
    if request.tool not in tools:
        raise HTTPException(status_code=400, detail=f"Tool '{request.tool}' not found")
    
    try:
        result = await tools[request.tool](**request.parameters)
        return {"result": result}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=3000)

Docker Deployment

dockerfile
# Dockerfile for Python MCP Server
FROM python:3.10-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

EXPOSE 3000

CMD ["uvicorn", "mcp_server:app", "--host", "0.0.0.0", "--port", "3000"]
yaml
# docker-compose.yml
version: '3'

services:
  mcp-server:
    build: .
    ports:
      - "3000:3000"
    environment:
      - LOG_LEVEL=info
      - AUTH_ENABLED=true
      - AUTH_API_KEY=your-secret-api-key
    volumes:
      - ./config:/app/config

Tool Implementation

Basic Tool Structure

javascript
// JavaScript tool implementation
const tools = {
  // Simple tool with direct implementation
  get_current_time: async (params) => {
    const { timezone = 'UTC' } = params;
    return {
      time: new Date().toLocaleString('en-US', { timeZone: timezone }),
      timezone
    };
  },
  
  // Tool that calls an external API
  fetch_stock_price: async (params) => {
    const { symbol } = params;
    try {
      const response = await fetch(`https://api.example.com/stocks/${symbol}`);
      const data = await response.json();
      return {
        symbol,
        price: data.price,
        currency: data.currency,
        timestamp: data.timestamp
      };
    } catch (error) {
      throw new Error(`Failed to fetch stock price: ${error.message}`);
    }
  }
};
python
# Python tool implementation
async def get_current_time(timezone: str = 'UTC') -> Dict[str, Any]:
    from datetime import datetime
    import pytz
    
    tz = pytz.timezone(timezone)
    current_time = datetime.now(tz)
    
    return {
        "time": current_time.strftime("%Y-%m-%d %H:%M:%S"),
        "timezone": timezone
    }

async def fetch_stock_price(symbol: str) -> Dict[str, Any]:
    import aiohttp
    
    async with aiohttp.ClientSession() as session:
        async with session.get(f"https://api.example.com/stocks/{symbol}") as response:
            if response.status != 200:
                raise Exception(f"API returned status code {response.status}")
            
            data = await response.json()
            
            return {
                "symbol": symbol,
                "price": data["price"],
                "currency": data["currency"],
                "timestamp": data["timestamp"]
            }

Tool Manifest

json
{
  "tools": [
    {
      "name": "get_current_time",
      "description": "Get the current time in a specified timezone",
      "parameters": {
        "type": "object",
        "properties": {
          "timezone": {
            "type": "string",
            "description": "Timezone identifier (e.g., 'UTC', 'America/New_York')"
          }
        },
        "required": []
      }
    },
    {
      "name": "fetch_stock_price",
      "description": "Get the current stock price for a given symbol",
      "parameters": {
        "type": "object",
        "properties": {
          "symbol": {
            "type": "string",
            "description": "Stock symbol (e.g., 'AAPL', 'MSFT')"
          }
        },
        "required": ["symbol"]
      }
    }
  ]
}

Advanced Tool with Authentication

javascript
// Tool that requires authentication
const authenticatedTools = {
  get_user_data: async (params, context) => {
    const { userId } = params;
    const { authToken } = context;
    
    if (!authToken) {
      throw new Error('Authentication required');
    }
    
    try {
      const response = await fetch(`https://api.example.com/users/${userId}`, {
        headers: {
          'Authorization': `Bearer ${authToken}`
        }
      });
      
      if (!response.ok) {
        throw new Error(`API returned status ${response.status}`);
      }
      
      return await response.json();
    } catch (error) {
      throw new Error(`Failed to fetch user data: ${error.message}`);
    }
  }
};

Authentication and Security

API Key Authentication

javascript
// Express middleware for API key authentication
function apiKeyAuth(req, res, next) {
  const apiKey = req.headers['x-api-key'];
  
  if (!apiKey || apiKey !== process.env.MCP_API_KEY) {
    return res.status(401).json({ error: 'Unauthorized' });
  }
  
  next();
}

// Apply middleware to MCP endpoint
app.post('/mcp', apiKeyAuth, async (req, res) => {
  // MCP request handling
});
python
# FastAPI API key authentication
from fastapi import Depends, HTTPException, Security
from fastapi.security.api_key import APIKeyHeader
import os

API_KEY = os.getenv("MCP_API_KEY")
api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)

async def get_api_key(api_key: str = Security(api_key_header)):
    if not api_key or api_key != API_KEY:
        raise HTTPException(status_code=401, detail="Invalid API Key")
    return api_key

@app.post("/mcp")
async def mcp_endpoint(request: MCPRequest, api_key: str = Depends(get_api_key)):
    # MCP request handling

JWT Authentication

javascript
// JWT authentication middleware
const jwt = require('jsonwebtoken');

function jwtAuth(req, res, next) {
  const authHeader = req.headers.authorization;
  
  if (!authHeader || !authHeader.startsWith('Bearer ')) {
    return res.status(401).json({ error: 'Unauthorized' });
  }
  
  const token = authHeader.split(' ')[1];
  
  try {
    const decoded = jwt.verify(token, process.env.JWT_SECRET);
    req.user = decoded;
    next();
  } catch (error) {
    return res.status(401).json({ error: 'Invalid token' });
  }
}

// Apply middleware to MCP endpoint
app.post('/mcp', jwtAuth, async (req, res) => {
  // MCP request handling with access to req.user
});
python
# FastAPI JWT authentication
from fastapi import Depends, HTTPException
from fastapi.security import OAuth2PasswordBearer
import jwt
from jwt.exceptions import PyJWTError

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
JWT_SECRET = os.getenv("JWT_SECRET")

async def get_current_user(token: str = Depends(oauth2_scheme)):
    try:
        payload = jwt.decode(token, JWT_SECRET, algorithms=["HS256"])
        username = payload.get("sub")
        if username is None:
            raise HTTPException(status_code=401, detail="Invalid authentication credentials")
    except PyJWTError:
        raise HTTPException(status_code=401, detail="Invalid authentication credentials")
    
    # Get user from database or return user info from token
    return {"username": username}

@app.post("/mcp")
async def mcp_endpoint(request: MCPRequest, current_user: dict = Depends(get_current_user)):
    # MCP request handling with access to current_user

Rate Limiting

javascript
// Rate limiting middleware
const rateLimit = require('express-rate-limit');

const mcpLimiter = rateLimit({
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 100, // limit each IP to 100 requests per windowMs
  message: 'Too many requests from this IP, please try again later'
});

// Apply rate limiting to MCP endpoint
app.post('/mcp', mcpLimiter, async (req, res) => {
  // MCP request handling
});
python
# FastAPI rate limiting with slowapi
from fastapi import Depends
from slowapi import Limiter
from slowapi.util import get_remote_address

limiter = Limiter(key_func=get_remote_address)
app = FastAPI()
app.state.limiter = limiter

@app.post("/mcp")
@limiter.limit("100/minute")
async def mcp_endpoint(request: MCPRequest, remote_addr: str = Depends(get_remote_address)):
    # MCP request handling

Advanced MCP Server Features

Tool Discovery

javascript
// Endpoint for tool discovery
app.get('/mcp/tools', apiKeyAuth, (req, res) => {
  const toolManifest = {
    tools: Object.keys(tools).map(toolName => {
      return {
        name: toolName,
        description: toolDescriptions[toolName] || '',
        parameters: toolParameters[toolName] || { type: 'object', properties: {} }
      };
    })
  };
  
  res.json(toolManifest);
});
python
# FastAPI tool discovery endpoint
@app.get("/mcp/tools")
async def get_tools(api_key: str = Depends(get_api_key)):
    tool_manifest = {
        "tools": [
            {
                "name": name,
                "description": tool_descriptions.get(name, ""),
                "parameters": tool_parameters.get(name, {"type": "object", "properties": {}})
            }
            for name in tools.keys()
        ]
    }
    
    return tool_manifest

Streaming Responses

javascript
// Express streaming response
app.post('/mcp/stream', apiKeyAuth, (req, res) => {
  const { tool, parameters } = req.body;
  
  if (!streamingTools[tool]) {
    return res.status(400).json({ error: `Streaming tool '${tool}' not found` });
  }
  
  // Set headers for streaming
  res.setHeader('Content-Type', 'text/event-stream');
  res.setHeader('Cache-Control', 'no-cache');
  res.setHeader('Connection', 'keep-alive');
  
  // Create streaming tool instance
  const stream = streamingTools[tool](parameters);
  
  // Handle data events
  stream.on('data', (data) => {
    res.write(`data: ${JSON.stringify(data)}\n\n`);
  });
  
  // Handle end event
  stream.on('end', () => {
    res.write('data: [DONE]\n\n');
    res.end();
  });
  
  // Handle errors
  stream.on('error', (error) => {
    res.write(`data: ${JSON.stringify({ error: error.message })}\n\n`);
    res.end();
  });
  
  // Handle client disconnect
  req.on('close', () => {
    stream.destroy();
  });
});
python
# FastAPI streaming response
from fastapi import Response
from fastapi.responses import StreamingResponse
import json
import asyncio

@app.post("/mcp/stream")
async def stream_mcp(request: MCPRequest, api_key: str = Depends(get_api_key)):
    if request.tool not in streaming_tools:
        raise HTTPException(status_code=400, detail=f"Streaming tool '{request.tool}' not found")
    
    async def event_generator():
        try:
            async for data in streaming_tools[request.tool](**request.parameters):
                yield f"data: {json.dumps(data)}\n\n"
                await asyncio.sleep(0.01)  # Small delay to prevent CPU hogging
            
            yield "data: [DONE]\n\n"
        except Exception as e:
            yield f"data: {json.dumps({'error': str(e)})}\n\n"
    
    return StreamingResponse(
        event_generator(),
        media_type="text/event-stream"
    )

Logging and Monitoring

javascript
// Winston logger setup
const winston = require('winston');

const logger = winston.createLogger({
  level: process.env.LOG_LEVEL || 'info',
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.json()
  ),
  transports: [
    new winston.transports.Console(),
    new winston.transports.File({ filename: 'mcp-server.log' })
  ]
});

// Logging middleware
function loggingMiddleware(req, res, next) {
  const start = Date.now();
  
  // Log request
  logger.info({
    type: 'request',
    method: req.method,
    path: req.path,
    tool: req.body.tool,
    requestId: req.headers['x-request-id'] || uuidv4()
  });
  
  // Capture response
  const originalSend = res.send;
  res.send = function(body) {
    const duration = Date.now() - start;
    
    // Log response
    logger.info({
      type: 'response',
      method: req.method,
      path: req.path,
      statusCode: res.statusCode,
      duration,
      requestId: req.headers['x-request-id'] || uuidv4()
    });
    
    return originalSend.call(this, body);
  };
  
  next();
}

// Apply logging middleware
app.use(loggingMiddleware);
python
# FastAPI logging middleware
import logging
import time
import uuid
from fastapi import Request

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler("mcp-server.log"),
        logging.StreamHandler()
    ]
)

logger = logging.getLogger("mcp-server")

@app.middleware("http")
async def logging_middleware(request: Request, call_next):
    request_id = request.headers.get("X-Request-ID", str(uuid.uuid4()))
    start_time = time.time()
    
    # Log request
    logger.info({
        "type": "request",
        "method": request.method,
        "path": request.url.path,
        "request_id": request_id
    })
    
    # Process request
    response = await call_next(request)
    
    # Log response
    duration = time.time() - start_time
    logger.info({
        "type": "response",
        "method": request.method,
        "path": request.url.path,
        "status_code": response.status_code,
        "duration": duration,
        "request_id": request_id
    })
    
    return response

Error Handling

javascript
// Error handling middleware
function errorHandler(err, req, res, next) {
  logger.error({
    type: 'error',
    error: err.message,
    stack: err.stack,
    path: req.path,
    requestId: req.headers['x-request-id'] || uuidv4()
  });
  
  res.status(500).json({
    error: 'Internal server error',
    requestId: req.headers['x-request-id'] || uuidv4()
  });
}

// Apply error handling middleware
app.use(errorHandler);
python
# FastAPI exception handlers
from fastapi.exceptions import RequestValidationError
from starlette.exceptions import HTTPException as StarletteHTTPException

@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
    logger.error({
        "type": "validation_error",
        "path": request.url.path,
        "errors": exc.errors(),
        "request_id": request.headers.get("X-Request-ID", str(uuid.uuid4()))
    })
    
    return JSONResponse(
        status_code=422,
        content={"detail": exc.errors(), "type": "validation_error"}
    )

@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request: Request, exc: StarletteHTTPException):
    logger.error({
        "type": "http_error",
        "path": request.url.path,
        "status_code": exc.status_code,
        "detail": exc.detail,
        "request_id": request.headers.get("X-Request-ID", str(uuid.uuid4()))
    })
    
    return JSONResponse(
        status_code=exc.status_code,
        content={"detail": exc.detail, "type": "http_error"}
    )

@app.exception_handler(Exception)
async def general_exception_handler(request: Request, exc: Exception):
    logger.error({
        "type": "server_error",
        "path": request.url.path,
        "error": str(exc),
        "request_id": request.headers.get("X-Request-ID", str(uuid.uuid4()))
    })
    
    return JSONResponse(
        status_code=500,
        content={"detail": "Internal server error", "type": "server_error"}
    )

Cloud Deployment

AWS Lambda Deployment

javascript
// serverless.yml for AWS Lambda deployment
service: mcp-server

provider:
  name: aws
  runtime: nodejs14.x
  region: us-east-1
  environment:
    MCP_API_KEY: ${env:MCP_API_KEY}
    LOG_LEVEL: info

functions:
  mcp:
    handler: handler.mcp
    events:
      - http:
          path: mcp
          method: post
          cors: true
  toolDiscovery:
    handler: handler.toolDiscovery
    events:
      - http:
          path: mcp/tools
          method: get
          cors: true
javascript
// handler.js for AWS Lambda
const serverless = require('serverless-http');
const express = require('express');
const app = express();

// ... MCP server implementation ...

// Export Lambda handlers
module.exports.mcp = serverless(app);
module.exports.toolDiscovery = serverless(app);

Azure Functions Deployment

javascript
// function.json for Azure Functions
{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": ["post"],
      "route": "mcp"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}
javascript
// index.js for Azure Functions
module.exports = async function (context, req) {
  // Validate request
  if (!req.body || !req.body.tool) {
    context.res = {
      status: 400,
      body: { error: "Missing required fields" }
    };
    return;
  }
  
  const { tool, parameters } = req.body;
  
  // Validate API key
  const apiKey = req.headers['x-api-key'];
  if (!apiKey || apiKey !== process.env.MCP_API_KEY) {
    context.res = {
      status: 401,
      body: { error: "Unauthorized" }
    };
    return;
  }
  
  try {
    // Execute tool
    if (!tools[tool]) {
      context.res = {
        status: 400,
        body: { error: `Tool '${tool}' not found` }
      };
      return;
    }
    
    const result = await tools[tool](parameters);
    
    context.res = {
      status: 200,
      body: { result }
    };
  } catch (error) {
    context.log.error(`Error executing tool ${tool}: ${error.message}`);
    
    context.res = {
      status: 500,
      body: { error: error.message }
    };
  }
};

Kubernetes Deployment

yaml
# kubernetes-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcp-server
  labels:
    app: mcp-server
spec:
  replicas: 3
  selector:
    matchLabels:
      app: mcp-server
  template:
    metadata:
      labels:
        app: mcp-server
    spec:
      containers:
      - name: mcp-server
        image: your-registry/mcp-server:latest
        ports:
        - containerPort: 3000
        env:
        - name: MCP_API_KEY
          valueFrom:
            secretKeyRef:
              name: mcp-secrets
              key: api-key
        - name: LOG_LEVEL
          value: "info"
        resources:
          requests:
            memory: "128Mi"
            cpu: "100m"
          limits:
            memory: "256Mi"
            cpu: "200m"
        livenessProbe:
          httpGet:
            path: /health
            port: 3000
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /health
            port: 3000
          initialDelaySeconds: 5
          periodSeconds: 5

---
apiVersion: v1
kind: Service
metadata:
  name: mcp-server-service
spec:
  selector:
    app: mcp-server
  ports:
  - port: 80
    targetPort: 3000
  type: LoadBalancer

---
apiVersion: v1
kind: Secret
metadata:
  name: mcp-secrets
type: Opaque
data:
  api-key: <base64-encoded-api-key>

Integration with AI Models

OpenAI Integration

javascript
// Client-side integration with OpenAI
const { OpenAI } = require('openai');

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY
});

async function callOpenAIWithMCP(prompt, mcpServerUrl, mcpApiKey) {
  const response = await openai.chat.completions.create({
    model: 'gpt-4',
    messages: [{ role: 'user', content: prompt }],
    tools: [
      {
        type: 'function',
        function: {
          name: 'mcp_server',
          description: 'Call the MCP server to access external tools and data',
          parameters: {
            type: 'object',
            properties: {
              tool: {
                type: 'string',
                description: 'The name of the tool to call'
              },
              parameters: {
                type: 'object',
                description: 'Parameters for the tool'
              }
            },
            required: ['tool']
          }
        }
      }
    ],
    tool_choice: 'auto'
  });
  
  // Check if the model wants to call a tool
  const message = response.choices[0].message;
  
  if (message.tool_calls && message.tool_calls.length > 0) {
    const toolCall = message.tool_calls[0];
    
    if (toolCall.function.name === 'mcp_server') {
      const { tool, parameters } = JSON.parse(toolCall.function.arguments);
      
      // Call MCP server
      const mcpResponse = await fetch(mcpServerUrl, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'X-API-Key': mcpApiKey
        },
        body: JSON.stringify({ tool, parameters })
      });
      
      const mcpResult = await mcpResponse.json();
      
      // Continue the conversation with the tool result
      const finalResponse = await openai.chat.completions.create({
        model: 'gpt-4',
        messages: [
          { role: 'user', content: prompt },
          message,
          {
            role: 'tool',
            tool_call_id: toolCall.id,
            content: JSON.stringify(mcpResult)
          }
        ]
      });
      
      return finalResponse.choices[0].message.content;
    }
  }
  
  return message.content;
}

Anthropic Integration

javascript
// Client-side integration with Anthropic
const { Anthropic } = require('@anthropic-ai/sdk');

const anthropic = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY
});

async function callAnthropicWithMCP(prompt, mcpServerUrl, mcpApiKey) {
  const response = await anthropic.messages.create({
    model: 'claude-3-opus-20240229',
    messages: [{ role: 'user', content: prompt }],
    tools: [
      {
        name: 'mcp_server',
        description: 'Call the MCP server to access external tools and data',
        input_schema: {
          type: 'object',
          properties: {
            tool: {
              type: 'string',
              description: 'The name of the tool to call'
            },
            parameters: {
              type: 'object',
              description: 'Parameters for the tool'
            }
          },
          required: ['tool']
        }
      }
    ]
  });
  
  // Check if the model wants to call a tool
  const message = response.content[0];
  
  if (message.type === 'tool_use' && message.name === 'mcp_server') {
    const { tool, parameters } = message.input;
    
    // Call MCP server
    const mcpResponse = await fetch(mcpServerUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'X-API-Key': mcpApiKey
      },
      body: JSON.stringify({ tool, parameters })
    });
    
    const mcpResult = await mcpResponse.json();
    
    // Continue the conversation with the tool result
    const finalResponse = await anthropic.messages.create({
      model: 'claude-3-opus-20240229',
      messages: [
        { role: 'user', content: prompt },
        { 
          role: 'assistant',
          content: [message]
        },
        {
          role: 'tool',
          name: 'mcp_server',
          content: JSON.stringify(mcpResult)
        }
      ]
    });
    
    return finalResponse.content[0].text;
  }
  
  return message.text;
}

Best Practices

Security Best Practices

  • Authentication: Always implement proper authentication for MCP servers
  • Authorization: Implement fine-grained access control for tools
  • Input Validation: Validate all input parameters to prevent injection attacks
  • Rate Limiting: Implement rate limiting to prevent abuse
  • Secrets Management: Use secure methods for storing and accessing API keys and secrets
  • HTTPS: Always use HTTPS for production deployments
  • Minimal Permissions: Follow the principle of least privilege for tool implementations

Performance Optimization

  • Caching: Implement caching for frequently used tool results
  • Connection Pooling: Use connection pooling for database and API connections
  • Asynchronous Processing: Use async/await for I/O-bound operations
  • Horizontal Scaling: Design for horizontal scaling to handle increased load
  • Timeout Handling: Implement proper timeout handling for external API calls
  • Resource Limits: Set appropriate CPU and memory limits for containers

Reliability

  • Error Handling: Implement comprehensive error handling and reporting
  • Retries: Add retry logic for transient failures
  • Circuit Breakers: Implement circuit breakers for external dependencies
  • Health Checks: Add health check endpoints for monitoring
  • Logging: Implement structured logging for troubleshooting
  • Monitoring: Set up monitoring and alerting for key metrics

Development Workflow

  • Version Control: Use version control for MCP server code
  • CI/CD: Implement continuous integration and deployment pipelines
  • Testing: Write unit and integration tests for tools
  • Documentation: Document all tools and their parameters
  • Code Reviews: Conduct thorough code reviews for security and quality
  • Semantic Versioning: Use semantic versioning for API changes

Troubleshooting

Common Issues

Authentication Failures

  • Cause: Incorrect API keys, expired tokens, or misconfigured authentication
  • Solution: Verify API keys, check token expiration, and ensure proper authentication configuration

Tool Execution Errors

  • Cause: Invalid parameters, external API failures, or bugs in tool implementation
  • Solution: Validate parameters, add error handling for external APIs, and test tools thoroughly

Performance Issues

  • Cause: Inefficient tool implementations, missing caching, or resource constraints
  • Solution: Optimize tool code, implement caching, and allocate appropriate resources

Integration Problems

  • Cause: Incorrect tool schemas, mismatched parameter types, or protocol misunderstandings
  • Solution: Verify tool schemas, ensure parameter types match, and follow the MCP specification

This comprehensive MCP Servers cheat sheet provides everything needed to build, deploy, and integrate Model Context Protocol servers. From basic setup to advanced deployment patterns, use these examples and best practices to create powerful, standardized connections between AI models and external tools and services.