Prompts2026年4月6日·1 分钟阅读

Build Your Own MCP Server — Step-by-Step Guide

Complete guide to building a custom MCP server from scratch. Covers the protocol, TypeScript and Python SDKs, tool definition, resource management, testing, and deployment patterns.

Agent 就绪

这个资产会安全暂存

这个资产会先安全暂存。复制的指令会要求 Agent 读取暂存文件,并在激活脚本、MCP 配置或全局配置前先确认。

Stage only · 17/100策略:需暂存
Agent 入口
任意 MCP/CLI Agent
类型
Mcp Config
安装
Stage only
信任
信任等级:Community
入口
Build Your Own MCP Server — Step-by-Step Guide
安全暂存命令
npx -y tokrepo@latest install 8cf17e75-0c30-4c57-971d-34d7284e4049 --target codex

先暂存文件;激活前需要读取暂存 README 和安装计划。

TL;DR
Step-by-step guide to building custom MCP servers with TypeScript or Python SDKs for extending AI agent capabilities.
§01

What it is

This is a comprehensive guide to building a custom Model Context Protocol (MCP) server from scratch. It covers the MCP protocol fundamentals, TypeScript and Python SDK usage, tool definition patterns, resource management, testing strategies, and deployment options. MCP servers extend AI agents like Claude Code with custom capabilities -- database access, API integrations, file processing, and more.

The guide targets developers who want to give their AI coding agents access to specific tools and data sources not covered by existing MCP servers. Building a custom server lets you expose your organization's internal APIs, databases, or services to AI agents securely.

§02

How it saves time or tokens

A custom MCP server lets AI agents interact with your systems directly instead of relying on you to copy-paste data between tools. The agent calls your server's tools, gets results, and incorporates them into its workflow. This eliminates the manual intermediary role and reduces the tokens spent on describing data that the agent could query directly.

§03

How to use

  1. Scaffold a new MCP server (TypeScript recommended):
npx @anthropic/create-mcp-server my-mcp-server
cd my-mcp-server && npm run dev
  1. Or use Python:
pip install mcp
  1. Define tools and add to your .mcp.json to test with Claude Code.
§04

Example

A minimal MCP server with a custom tool 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-server', version: '1.0.0' });

server.tool(
  'lookup_user',
  'Find a user by email address',
  { email: z.string().email() },
  async ({ email }) => {
    const user = await db.findUserByEmail(email);
    return { content: [{ type: 'text', text: JSON.stringify(user) }] };
  }
);

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

This server exposes a lookup_user tool that Claude Code can call during conversations.

§05

Related on TokRepo

§06

Common pitfalls

  • MCP servers communicate via stdio. Logging to stdout breaks the protocol. Use stderr for debug output.
  • Tool descriptions are sent to the LLM as context. Vague descriptions lead to incorrect tool usage. Write clear, specific descriptions for each tool.
  • Input validation is critical. The LLM may send unexpected parameter values. Use Zod schemas (TypeScript) or Pydantic models (Python) to validate all inputs.

常见问题

What programming languages can I use to build an MCP server?+

The official SDKs support TypeScript and Python. TypeScript is recommended for its type safety and the mature @modelcontextprotocol/sdk package. Python uses the mcp package. Both support the full MCP specification.

How do I test my MCP server?+

Add your server to the .mcp.json file and restart Claude Code. The agent can then call your tools during conversations. Use stderr logging to debug tool execution without breaking the stdio protocol.

Can MCP servers access databases?+

Yes. Your MCP server can connect to any database your programming language supports. Define tools that accept query parameters, execute database operations, and return results to the AI agent.

How do I deploy an MCP server?+

MCP servers run as local processes alongside the AI agent. For Claude Code, they are configured in .mcp.json and launched automatically. For shared servers, you can deploy them as HTTP endpoints using the SSE transport.

What is the difference between tools and resources in MCP?+

Tools are actions the agent can invoke (like API calls or database queries). Resources are data the agent can read (like file contents or configuration). Tools are active operations; resources are passive data sources.

引用来源 (3)
🙏

来源与感谢

Based on the MCP Specification by Anthropic.

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产