使用说明
MCP Base——一种通用模型上下文协议框架
此文件夹包含用于构建AI驱动应用程序的模型上下文协议(MCP)的通用基础实现。它提供了一种创建MCP服务器和客户端的标准化方法,可用于将LLM集成到您的应用程序中。
📋 特性
- 标准化MCP服务器:支持HTTP和stdio传输的基础服务器实现
- 通用MCP客户端:用于连接到任何MCP服务器的客户端
- Olama集成:使用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/目录 - 定义提示模式和参数
- 实现提示模板
- 在服务器中注册提示
📄 许可证
麻省理工学院