公式のPython SDKを使用すると、MCPサーバーの構築がはるかにシンプルになります。ツール用の複雑なJSONスキーマを手動で記述する代わりに、SDKがデコレータと型ヒントを使ってその複雑さをすべて処理してくれます。
この例では、メモリ内に保存されたドキュメントを管理するMCPサーバーを作成します。このサーバーは2つの重要なツールを提供します。1つはドキュメントの内容を読み取るツール、もう1つは検索・置換操作を通じてドキュメントを更新するツールです。
MCPサーバーのセットアップ
Python MCP SDKを使うと、サーバーの作成が驚くほど簡単になります。わずか1行で完全なMCPサーバーを初期化できます。
```
from mcp.server.fastmcp import FastMCP
from pydantic import Field
mcp = FastMCP("DocumentMCP", log_level="ERROR")
```
この実装では、ドキュメントはシンプルなPythonの辞書に保存されます。キーはドキュメントID、値にはドキュメントの内容が含まれます。
```
docs = {
"deposition.md": "This deposition covers the testimony of Angela Smith, P.E.",
"report.pdf": "The report details the state of a 20m condenser tower.",
"financials.docx": "These financials outline the project's budget and expenditure",
"outlook.pdf": "This document presents the projected future performance of the",
"plan.md": "The plan outlines the steps for the project's implementation.",
"spec.txt": "These specifications define the technical requirements for the equipment"
}
```