MCPサーバーを自作したい——この記事では、Python/TypeScriptで独自のMCPサーバーを作る手順を、コピペで動くコード付きで解説する。自社の業務システムとClaude Codeを接続できるようになる。
MCPサーバー自作の全体像
| Step | やること | 所要時間 |
|---|---|---|
| 1 | SDKのインストール | 1分 |
| 2 | サーバーのコードを書く | 10〜30分 |
| 3 | Claude Codeに登録 | 2分 |
| 4 | 動作確認 | 3分 |
最短15分で自作MCPサーバーが動く。

作り方①|Python版
Step 1: SDKインストール
pip install mcp
Step 2: サーバーコード
# my_mcp_server.py
from mcp.server import Server
from mcp.types import Tool, TextContent
app = Server("my-server")
@app.tool()
async def hello(name: str) -> list[TextContent]:
"""挨拶をする"""
return [TextContent(type="text", text=f"こんにちは、{name}さん!")]
@app.tool()
async def add(a: int, b: int) -> list[TextContent]:
"""2つの数を足す"""
return [TextContent(type="text", text=f"{a} + {b} = {a + b}")]
if __name__ == "__main__":
import asyncio
asyncio.run(app.run())
Step 3: Claude Codeに登録
# Claude Codeセッション内で
/mcp
# 「Add Server」→「stdio」→以下を入力
# Name: my-server
# Command: python3 /path/to/my_mcp_server.py
Step 4: 動作確認
# Claude Codeで
> helloツールを使って「太郎」に挨拶して
# → 「こんにちは、太郎さん!」
> addツールで 3 と 5 を足して
# → 「3 + 5 = 8」
作り方②|TypeScript版
Step 1: SDKインストール
npm install @modelcontextprotocol/sdk
Step 2: サーバーコード
// my-mcp-server.ts
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
const server = new Server({ name: "my-server", version: "1.0.0" }, {
capabilities: { tools: {} }
});
server.setRequestHandler("tools/list", async () => ({
tools: [{
name: "hello",
description: "挨拶をする",
inputSchema: {
type: "object",
properties: { name: { type: "string", description: "名前" } },
required: ["name"]
}
}]
}));
server.setRequestHandler("tools/call", async (request) => {
if (request.params.name === "hello") {
const name = request.params.arguments.name;
return { content: [{ type: "text", text: `こんにちは、${name}さん!` }] };
}
});
const transport = new StdioServerTransport();
await server.connect(transport);
TypeScript版 Step 3: Claude Codeに登録
Python版と同じ手順。/mcp→「Add Server」→npx ts-node /path/to/my-mcp-server.tsを入力。
TypeScript版 Step 4: 動作確認
# Claude Codeで
> helloツールを使って「花子」に挨拶して
# → 「こんにちは、花子さん!」
実用的な自作例|社内DBに接続するMCPサーバー
# 社内の売上DBに接続するMCPサーバー(Python)
import sqlite3
from mcp.server import Server
from mcp.types import Tool, TextContent
app = Server("sales-db")
@app.tool()
async def query_sales(month: str) -> list[TextContent]:
"""指定月の売上合計を返す"""
conn = sqlite3.connect("sales.db")
cursor = conn.execute(
"SELECT SUM(amount) FROM sales WHERE month = ?", (month,)
)
total = cursor.fetchone()[0] or 0
conn.close()
return [TextContent(type="text", text=f"{month}の売上合計: {total:,}円")]
これでClaude Codeに「今月の売上は?」と聞くだけで、社内DBから数値を取得して回答してくれるようになる。
自作 vs 公開サーバー|どっちを使うべき?
| 状況 | おすすめ |
|---|---|
| GitHub/Slack等の汎用ツール連携 | 公開MCPサーバーを使う |
| 自社の業務システムとの連携 | 自作する |
| 既存のREST APIをラップしたい | 自作する |
| 公開サーバーにない機能が必要 | 自作する |
公開MCPサーバーの一覧は「MCPサーバーおすすめ20選」を参照。MCPの基礎は「MCPサーバーとは?」で解説。
公式リソース
よくある質問
Q: PythonとTypeScriptどっちがおすすめ?
普段使う言語で選ぶ。機能的な差はない。Pythonの方がコード量が少なく初心者向き。
Q: 自作MCPサーバーの公開方法は?
GitHubにリポジトリを作って公開すればOK。npmやPyPIにパッケージとして公開することも可能。
まとめ|自作MCPサーバーは15分で動く
MCPサーバーの自作はPython/TypeScriptのSDKを使えば15分で完成。自社DBや業務システムをClaude Codeに接続したい場合は自作一択。Claude Codeの使い方は「使い方ガイド」で解説。

