FastMCPを使用したMCP(Model Context Protocol)サーバーのモック実装です。 Difyや他のAIクライアントから外部システム連携を試すために使用できます。
このMCPサーバーは以下のツールを提供します:
- get_qa_history: 過去のQA集を取得(モックデータ)
- get_users: ユーザー名簿を取得(モックデータ)
docker compose up -dhttp://localhost:8000/sse
MCPサーバーはSSE(Server-Sent Events)トランスポートで起動しています。
Difyの設定で以下のように指定します:
- MCP Server URL:
http://localhost:8000/sse - Transport Type:
SSE
import asyncio
from fastmcp import Client
async def main():
async with Client("http://localhost:8000/sse") as client:
# QA履歴を取得
result = await client.call_tool(
name="get_qa_history",
arguments={}
)
print(result)
# ユーザー一覧を取得
result = await client.call_tool(
name="get_users",
arguments={}
)
print(result)
asyncio.run(main())docker compose logs -f mcp-serverdocker compose down.
├── docker-compose.yml
├── mcp_server/
│ ├── main.py # MCPサーバー本体
│ ├── requirements.txt # Python依存パッケージ
│ └── Dockerfile # Dockerイメージ定義
└── README.md
mcp_server/main.pyを編集して、新しいツールを追加したり、既存のモックデータを変更できます。
@mcp.tool()
def your_custom_tool(param: str) -> dict:
"""あなたのカスタムツール"""
return {"result": "カスタム結果"}変更後は再ビルドが必要です:
docker compose down
docker compose build
docker compose up -d