MCP Base - 汎用モデルコンテキストプロトコルフレームワーク
このフォルダには、AIを活用したアプリケーションを構築するためのモデルコンテキストプロトコル(MCP)の汎用的な基本実装が含まれています。これにより、LLMをアプリケーションに統合するために使用できるMCPサーバーとクライアントを標準化された方法で作成できます。
📋 機能
- 標準化されたMCPサーバーおよびstdioトランスポートをサポートする基本サーバー実装
- 汎用MCPクライアント:任意のMCPサーバーに接続するためのクライアント
- Ollama 統合: Ollama で埋め込みとテキストを生成するためのすぐに使えるサービス
- Supabase 統合: Supabase ベクター データベースの組み込みサポート
- モジュラー設計: リソース、ツール、プロンプトを明確に整理した構造
- サンプルテンプレート: すぐに開始できるようにするための実装例
🛠️ ディレクトリ構造
_mcp-base/
├── server.ts # Main MCP server implementation
├── client.ts # Generic MCP client
├── utils/ # Utility services
│ ├── ollama_embedding.ts # Embedding generation with Ollama
│ └── ollama_text_generation.ts # Text generation with Ollama
├── tools/ # Tool implementations
│ └── sample-tool.ts # Example tool template
├── resources/ # Resource implementations
│ └── sample-resource.ts # Example resource template
├── prompts/ # Prompt implementations
│ └── sample-prompt.ts # Example prompt template
└── README.md # This documentation
🚀 はじめに
前提条件
- Node.js と npm/pnpm
- ローカル埋め込みとテキスト生成のための Ollama
- ベクターストレージ用のSupabaseアカウント
環境設定
次の変数を含む.env
ファイルを作成します。
PORT=3000
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_SERVICE_KEY=your-service-key
OLLAMA_URL=http://localhost:11434
OLLAMA_EMBED_MODEL=nomic-embed-text
OLLAMA_LLM_MODEL=llama3
SERVER_MODE=http # 'http' or 'stdio'
サーバーの初期化
- 必要なモジュールをインポートする
- リソース、ツール、プロンプトを登録する
- サーバーを起動する
// Import base server and utilities
import server from "./server";
import { registerSampleResources } from "./resources/sample-resource";
import { registerSampleTool } from "./tools/sample-tool";
import { registerSamplePrompts } from "./prompts/sample-prompt";
// Initialize database if needed
async function initializeDatabase() {
// Your database initialization logic
}
// Register your components
registerSampleResources(server, supabase);
registerSampleTool(server, textGenerator, embeddings, supabase);
registerSamplePrompts(server, supabase);
// Start the server
startServer();
クライアントの使用状況
import MCPClient from "./client";
// Create a client instance
const client = new MCPClient({
serverUrl: "http://localhost:3000",
});
// Example: Call a tool
async function callSampleTool() {
const result = await client.callTool("sample-tool", {
query: "example query",
maxResults: 5,
});
console.log(result);
}
// Example: Read a resource
async function readResource() {
const items = await client.readResource("items://all");
console.log(items);
}
// Example: Get a prompt
async function getPrompt() {
const prompt = await client.getPrompt("simple-prompt", {
task: "Explain quantum computing",
});
console.log(prompt);
}
// Don't forget to disconnect when done
await client.disconnect();
📚 フレームワークの拡張
新しいツールの作成
tools/
ディレクトリに新しいファイルを作成する- Zodを使用してツールの機能とスキーマを定義する
- ツールロジックを実装する
- ツールをサーバーに登録する
新しいリソースの作成
resources/
ディレクトリに新しいファイルを作成する- リソースのエンドポイントとスキーマを定義する
- リソースロジックを実装する
- サーバーにリソースを登録する
新しいプロンプトの作成
prompts/
ディレクトリに新しいファイルを作成する- プロンプトのスキーマとパラメータを定義する
- プロンプトテンプレートを実装する
- サーバーにプロンプトを登録する
📄 ライセンス
マサチューセッツ工科大学