Claude Code: MCP (Model Context Protocol)

Last updated: 2026-08-31

MCP lets Claude Code go beyond reading/writing local files — through MCP servers, it can connect to databases, operate browsers, call APIs, with unlimited capability expansion.

💡 Tip: MCP (Model Context Protocol) is an open protocol by Anthropic that lets AI models safely connect to external data sources and tools. Claude Code natively supports MCP.

📋 Prerequisites: Chapter 11 - Permission Configuration

1. What You'll Learn


2. MCP Core Concepts

(1) Architecture Overview

100%
graph LR
    CC[Claude Code] <--> MCP[MCP Client]
    MCP <--> S1[Filesystem Server]
    MCP <--> S2[Database Server]
    MCP <--> S3[GitHub Server]
    MCP <--> S4[Browser Server]
Concept Description Analogy
MCP Host Program running the AI model Browser
MCP Client Communicates with Server HTTP Client
MCP Server Provides tools and data HTTP Server
Tool Operation exposed by Server API Endpoint
Resource Data exposed by Server API Resource

(2) MCP Capabilities

Ability Without MCP With MCP
Read local files ✅ Built-in ✅ Built-in
Write local files ✅ Built-in ✅ Built-in
Execute Shell ✅ Built-in ✅ Built-in
Query database ✅ PostgreSQL MCP
Operate GitHub ❌ (CLI only) ✅ GitHub MCP
Browser automation ✅ Browser MCP
Search the web ✅ Search MCP

▶ Example 1: MCP Workflow

TEXT 📖 Display only
# Without MCP: Manual database query
> Check how many records are in the users table
Claude Code: I can't access the database directly. Run:
  psql -c "SELECT COUNT(*) FROM users"

# With MCP (PostgreSQL Server): Direct query
> Check how many records are in the users table
Claude Code: [Query via PostgreSQL MCP]
  users table has 12,450 records.
  Active users: 8,920 (71.6%),
  Registered in last 7 days: 342.

3. MCP Server Configuration

(1) Global Configuration

JSON
// ~/.claude/mcp_settings.json
{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@anthropic/mcp-filesystem", "/home/user/projects"]
    },
    "postgres": {
      "command": "npx",
      "args": ["-y", "@anthropic/mcp-postgres"],
      "env": {
        "DATABASE_URL": "postgresql://user:pass@localhost:5432/mydb"
      }
    }
  }
}

(2) Project-Level Configuration

JSON
// .claude/mcp_settings.json
{
  "mcpServers": {
    "github": {
      "command": "npx",
      "args": ["-y", "@anthropic/mcp-github"],
      "env": {
        "GITHUB_TOKEN": "ghp_xxxxx"
      }
    }
  }
}

4. Common MCP Servers

(1) Official MCP Servers

Server Function Install Command
Filesystem Enhanced file operations npx -y @anthropic/mcp-filesystem
PostgreSQL Database queries npx -y @anthropic/mcp-postgres
GitHub GitHub API operations npx -y @anthropic/mcp-github
GitLab GitLab API operations npx -y @anthropic/mcp-gitlab
Browser Browser automation npx -y @anthropic/mcp-browser
Brave Search Web search npx -y @anthropic/mcp-brave-search

▶ Example 2: PostgreSQL MCP Practice

TEXT 📖 Display only
> Query user registrations in the last 7 days, grouped by day

Claude Code: [Using PostgreSQL MCP]
  → Executing SQL query...
  Results: 7-day breakdown with daily counts

5. Custom MCP Server

▶ Example 3: Create Simple MCP Server

TYPESCRIPT
import { Server } from "@anthropic/mcp";

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

server.tool("get_weather", "Get current weather for a city", {
  city: { type: "string", description: "City name" },
}, async ({ city }) => {
  const response = await fetch(
    `https://api.weather.com/current?city=${city}`
  );
  const data = await response.json();
  return {
    content: [{
      type: "text",
      text: `${city}: ${data.temperature}°C, ${data.condition}`
    }]
  };
});

server.start();

6. MCP Security and Permissions

Practice Description
Least privilege Only configure necessary MCP servers
Environment variables Tokens/passwords via env vars, not hardcoded
Read-only first Database MCP prefers read-only connections
Audit logs Record MCP tool call history
Network isolation Don't expose production databases to MCP

❓ FAQ

Q Difference between MCP and API calls?
A MCP is a standardized protocol providing unified tool discovery and invocation. API calls need hardcoded code; MCP lets Claude Code dynamically discover and use tools.
Q Do MCP servers slow down Claude Code?
A Slightly slower at startup (loading MCP config), but only MCP tool calls consume time during runtime. Regular operations unaffected.
Q How to debug MCP connection issues?
A Run claude /mcp to view connected servers and tools. Check config file paths and environment variables.
Q Can MCP connect to production databases?
A Technically yes, but strongly not recommended. Use read-only replicas or dev databases.

📖 Summary


📝 Exercises

  1. Basic (⭐): Configure Filesystem MCP server, verify enhanced file operations work.
  2. Intermediate (⭐⭐): Configure PostgreSQL MCP, query and analyze database results with Claude Code.
  3. Advanced (⭐⭐⭐): Create a custom MCP server providing specific business functionality, integrate into Claude Code.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏