Master Claude Code in a Week (Part 8): MCP Protocol

· 5 min read

This is the eighth part of the "Master Claude Code in a Week" series. In this article, we'll explore MCP Protocol — how to connect Claude Code with external tools and APIs.

What is MCP?

Model Context Protocol (MCP) is a protocol that allows Claude Code to communicate with external services. Instead of just working with local files, MCP lets Claude access databases, APIs, cloud services, and more.

┌────────────────────────────────────────────────────────────────┐
│                       MCP PROTOCOL                              │
├────────────────────────────────────────────────────────────────┤
│                                                                 │
│   Claude Code                                                   │
│       │                                                        │
│       ▼                                                        │
│   ┌─────────────────────────────────────────────┐             │
│   │              MCP Client                      │             │
│   └─────────────────────────────────────────────┘             │
│       │           │           │           │                    │
│       ▼           ▼           ▼           ▼                    │
│   ┌───────┐   ┌───────┐   ┌───────┐   ┌───────┐              │
│   │GitHub │   │  DB   │   │ Files │   │ Custom│              │
│   │Server │   │Server │   │Server │   │Server │              │
│   └───────┘   └───────┘   └───────┘   └───────┘              │
│       │           │           │           │                    │
│       ▼           ▼           ▼           ▼                    │
│   ┌───────┐   ┌───────┐   ┌───────┐   ┌───────┐              │
│   │GitHub │   │ MySQL │   │  AWS  │   │  API  │              │
│   │  API  │   │Postgre│   │   S3  │   │Service│              │
│   └───────┘   └───────┘   └───────┘   └───────┘              │
│                                                                 │
└────────────────────────────────────────────────────────────────┘

Installing MCP Servers

Via CLI

# Add GitHub MCP server
export GITHUB_TOKEN="ghp_xxxxxxxxxxxx"
claude mcp add github -- npx -y @modelcontextprotocol/server-github

# Add filesystem MCP server
claude mcp add filesystem -- npx -y @modelcontextprotocol/server-filesystem ~/projects

# Add PostgreSQL MCP server
export DATABASE_URL="postgresql://user:pass@localhost:5432/mydb"
claude mcp add postgres -- npx -y @modelcontextprotocol/server-postgres

Via Configuration File

Location: .mcp.json (project) or ~/.mcp.json (global)

{
  "mcpServers": {
    "github": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-github"],
      "env": {
        "GITHUB_TOKEN": "${GITHUB_TOKEN}"
      }
    },
    "postgres": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-postgres"],
      "env": {
        "DATABASE_URL": "${DATABASE_URL}"
      }
    }
  }
}

1. GitHub Server

Install:

export GITHUB_TOKEN="ghp_xxxxxxxxxxxx"
claude mcp add github -- npx -y @modelcontextprotocol/server-github

Capabilities:

  • List repositories
  • Read/Create issues
  • Read/Create pull requests
  • Access repository contents

Example usage:

User: "Create an issue for the performance bug we discussed"

Claude: [Uses GitHub MCP]
Created issue #42: "Performance degradation in API endpoints"

2. PostgreSQL Server

Install:

export DATABASE_URL="postgresql://user:pass@localhost:5432/mydb"
claude mcp add postgres -- npx -y @modelcontextprotocol/server-postgres

Capabilities:

  • Execute SQL queries
  • List tables
  • Describe schema

Example usage:

User: "Show me the top 10 users by order count"

Claude: [Uses PostgreSQL MCP]
SELECT u.name, COUNT(o.id) as order_count
FROM users u
JOIN orders o ON o.user_id = u.id
GROUP BY u.id
ORDER BY order_count DESC
LIMIT 10;

3. Filesystem Server

Install:

claude mcp add filesystem -- npx -y @modelcontextprotocol/server-filesystem ~/projects

Capabilities:

  • Read/Write files
  • List directories
  • Search files

Configuration Examples

Multi-server Configuration

{
  "mcpServers": {
    "github": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-github"],
      "env": {
        "GITHUB_TOKEN": "${GITHUB_TOKEN}"
      }
    },
    "postgres-main": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-postgres"],
      "env": {
        "DATABASE_URL": "${DATABASE_URL_MAIN}"
      }
    },
    "slack": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-slack"],
      "env": {
        "SLACK_BOT_TOKEN": "${SLACK_BOT_TOKEN}"
      }
    }
  }
}

Custom MCP Server

You can create your own MCP server for internal APIs:

// my-mcp-server/index.js
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";

const server = new Server({
  name: "my-api-server",
  version: "1.0.0",
});

// Define tools
server.setRequestHandler("tools/list", async () => ({
  tools: [
    {
      name: "get_users",
      description: "Get list of users from internal API",
      inputSchema: {
        type: "object",
        properties: {
          limit: { type: "number" },
        },
      },
    },
  ],
}));

// Handle tool calls
server.setRequestHandler("tools/call", async (request) => {
  const { name, arguments: args } = request.params;
  
  if (name === "get_users") {
    const response = await fetch(`${process.env.API_URL}/users?limit=${args.limit || 10}`);
    return { content: [{ type: "text", text: JSON.stringify(await response.json()) }] };
  }
});

const transport = new StdioServerTransport();
await server.connect(transport);

Register Custom Server

{
  "mcpServers": {
    "my-api": {
      "command": "node",
      "args": ["./my-mcp-server/index.js"],
      "env": {
        "API_URL": "${MY_API_URL}"
      }
    }
  }
}

Use Cases

1. Automated PR Review with GitHub

User: "Review the open PRs in our repo and summarize issues"

Claude: [Uses GitHub MCP]
1. Fetches open PRs from repository
2. Analyzes changes in each PR
3. Generates summary with recommendations

2. Database Analysis

User: "Analyze slow queries and suggest indexes"

Claude: [Uses PostgreSQL MCP]
1. Queries pg_stat_statements
2. Identifies slow queries
3. Suggests indexes

3. Multi-service Orchestration

User: "When a critical bug is reported, create GitHub issue and Slack alert"

Claude:
1. [GitHub MCP] Creates issue
2. [Slack MCP] Posts alert

Security Best Practices

1. Use environment variables

{
  "mcpServers": {
    "github": {
      "env": {
        "GITHUB_TOKEN": "${GITHUB_TOKEN}"  // ✅ Good
      }
    }
  }
}

2. Limit permissions

# Use fine-grained tokens with minimal permissions
# Read-only for repos that don't need write

3. Project-specific configs

# Don't put production credentials in global config
# Use project-specific .mcp.json with dev credentials

Troubleshooting

MCP server not starting

# Check if server can start manually
npx -y @modelcontextprotocol/server-github

# Check environment variables
echo $GITHUB_TOKEN

Connection errors

# Verify network connectivity
curl https://api.github.com/user -H "Authorization: Bearer $GITHUB_TOKEN"

Summary

MCP opens up the world of external integrations:

  • ✅ Connect to databases, APIs, cloud services
  • ✅ Real-time data access
  • ✅ Custom MCP servers for internal tools
  • ✅ Multi-service orchestration

Next Up

In the next part, we'll explore Subagents — how to create and use specialized AI assistants.

References


This series is translated and expanded from claude-howto — MIT License.

Comments