Using MCP Servers in Claude Code
Stay lazy, code smarter
New tutorials and tips straight to your inbox
Using MCP Servers in Claude Code
Learn how to configure and use MCP (Model Context Protocol) servers to give Claude Code access to external tools and data sources.
What is MCP?
The Model Context Protocol (MCP) is a standard interface that lets Claude Code communicate with external tools and data sources. Think of it like USB for AI — any tool that implements the protocol can plug into Claude Code.
Why MCP Matters
Out of the box, Claude Code can read files and run commands. MCP extends that to:
- Databases — Query your dev database, inspect schemas, run migrations
- APIs — Hit internal services, check deployment status, read documentation
- Custom tools — Anything you can write a function for
Setting Up Your First MCP Server
Create a .mcp.json file in your project root:
{
"mcpServers": {
"weather": {
"command": "node",
"args": ["./mcp-servers/weather/dist/index.js"]
}
}
}
Building a Simple MCP Server
Here's a minimal MCP server in TypeScript:
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
const server = new McpServer({
name: "my-tools",
version: "1.0.0",
});
server.tool(
"lookup_user",
"Look up a user by email",
{ email: z.string().email() },
async ({ email }) => ({
content: [{
type: "text" as const,
text: JSON.stringify({ id: "123", email, name: "Jane" }),
}],
})
);
const transport = new StdioServerTransport();
server.connect(transport);
How Claude Discovers Tools
When you start a session, Claude Code reads your .mcp.json, starts each server, and discovers what tools are available. When you ask a question that needs external data, Claude automatically calls the right tool.
Best Practices
- One tool per task — Don't create a
do_everythingtool. Keep them focused. - Validate inputs with Zod — Always validate what Claude sends.
- Return structured JSON — Easier for Claude to parse than prose.
- Handle errors gracefully — Return helpful messages so Claude can retry or self-correct.
Popular MCP Servers
The ecosystem is growing fast. Some useful ones:
- @modelcontextprotocol/server-filesystem — Enhanced file operations
- @modelcontextprotocol/server-github — GitHub API access
- @modelcontextprotocol/server-postgres — PostgreSQL queries
- Community servers for Slack, Linear, Notion, and more