Claude MCPとは
Claude MCP(Model Context Protocol)は、Anthropicが提供する標準プロトコルで、Claude AIと外部ツール・データソース間の通信を簡素化します。API直接呼び出しやカスタムインテグレーションの複雑さを解消し、統一的なインターフェースを提供します。
- 標準化された通信:JSON-RPC 2.0ベースの一貫したプロトコル
- ツール抽象化:API、データベース、ローカルファイルを統一的に扱う
- サーバーモデル:MCP Serverとして機能拡張を提供
MCP Serverの基本構造
MCP Serverは、stdioまたはSSE(Server-Sent Events)を通じてClaudeと通信するプロセスです。以下の主要コンポーネントで構成されますされます:
{
"jsonrpc": "2.0",
"method": "tools/list",
"params": {},
"id": 1
}
- tools/list:利用可能なツール一覧を返す
- tools/call:ツールを実行する
- resources/list:利用可能なリソース一覧
- resources/read:リソースの内容を読み込む
実装例:ファイルシステムMCP Server
以下は、PythonでシンプルなファイルシステムMCP Serverを実装する例です:
#!/usr/bin/env python3
import sys
import json
import os
def handle_tools_list():
return {
"jsonrpc": "2.0",
"result": {
"tools": [
{
"name": "read_file",
"description": "ファイルの内容を読み込む",
"inputSchema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "ファイルパス"
}
},
"required": ["path"]
}
}
]
},
"id": None
}
def handle_tools_call(params):
tool_name = params.get("name")
arguments = params.get("arguments", {})
if tool_name == "read_file":
path = arguments.get("path")
try:
with open(path, "r") as f:
content = f.read()
return {
"jsonrpc": "2.0",
"result": {
"content": [
{
"type": "text",
"text": content
}
]
},
"id": None
}
except Exception as e:
return {
"jsonrpc": "2.0",
"error": {
"code": -1,
"message": str(e)
},
"id": None
}
def main():
for line in sys.stdin:
try:
request = json.loads(line.strip())
method = request.get("method")
if method == "initialize":
response = {
"jsonrpc": "2.0",
"result": {
"protocolVersion": "2024-11-05",
"serverInfo": {
"name": "filesystem-server",
"version": "1.0.0"
},
"capabilities": {
"tools": {}
"
}
},
"id": request.get("id")
}
elif method == "tools/list":
response = handle_tools_list()
elif method == "tools/call":
response = handle_tools_call(request.get("params", {}))
else:
response = {
"jsonrpc": "2.0",
"error": {
"code": -32601,
"message": "Method not found"
},
"id": request.get("id")
}
print(json.dumps(response))
sys.stdout.flush()
except Exception as e:
error_response = {
"jsonrpc": "2.0",
"error": {
"code": -32700,
"message": str(e)
},
"id": None
}
print(json.dumps(error_response))
sys.stdout.flush()
if __name__ == "__main__":
main()
MCP Serverのデプロイ方法
作成したMCP ServerをClaude Desktopで使用するには、以下の設定ファイルを作成します:
{
"mcpServers": {
"filesystem": {
"command": "python3",
"args": ["/path/to/filesystem_server.py"]
}
}
}
応用例:データベースMCP Server
データベースに接続し、SQLクエリを実行するMCP Serverも簡単に実装できます。以下はSQLiteを使用する例の主要部分です:
import sqlite3
def handle_query(arguments):
query = arguments.get("query")
try:
conn = sqlite3.connect("database.db")
cursor = conn.cursor()
cursor.execute(query)
rows = cursor.fetchall()
conn.close()
return {
"jsonrpc": "2.0",
"result": {
"content": [
{
"type": "text",
"text": json.dumps(rows)
}
]
},
"id": None
}
except Exception as e:
return {
"jsonrpc": "2.0",
"error": {
"code": -1,
"message": str(e)
},
"id": None
}
ベストプラクティス
- エラーハンドリング:すべてのコマンドで適切なエラー処理を実装する
- 入力バリデーション:引数を検証し、不正な入力を拒否する
- 非同期処理:長時間実行される操作はバックグラウンドで処理する
- ログ出力:デバッグ用に詳細なログを記録する
よくある問題と解決方法
Q: MCP Serverが認識されない
A: 設定ファイルのパスと権限を確認してください。コマンドの絶対パスを使用することを推奨します。
Q: JSON-RPCエラーが発生する
A: リクエストとレスポンスのJSONフォーマットを確認してください。jsonrpcバージョンが”2.0″であることを確認します。
公式ドキュメント:Model Context Protocol — Anthropic Documentation
次に読む
LangGraph実践ガイド:複雑なAIエージェントの構築方法
この記事を読んで導入イメージが固まってきた方へ
UravationではAIエージェント導入の研修・コンサルを行っています。
