Gemini上下文MCP服务器
一个强大的MCP(模型上下文协议)服务器实现,利用Gemini的上下文管理和缓存功能。该服务器最大限度地提高了Gemini 2M令牌上下文窗口的价值,同时提供了高效缓存大型上下文的工具。
🚀 特性
上下文管理
- 支持高达2M的令牌上下文窗口 -利用Gemini广泛的上下文功能
- 基于会话的对话 -在多个交互中保持对话状态
- 智能上下文跟踪 -使用元数据添加、检索和搜索上下文
- 语义搜索 -使用语义相似性查找相关上下文
- 自动上下文清理 -会话和上下文自动过期
API缓存
- 大提示缓存 -高效地重用大型系统提示和指令
- 成本优化 -降低常用上下文的令牌使用成本
- TTL管理 -控制缓存过期时间
- 自动清理 -过期的缓存会自动删除
🏁 快速开始
先决条件
- 已安装Node.js 18+
- Gemini API密钥(在这里买一个)
安装
# Clone the repository
git clone https://github.com/ogoldberg/gemini-context-mcp-server
cd gemini-context-mcp-server
# Install dependencies
npm install
# Copy environment variables example
cp .env.example .env
# Add your Gemini API key to .env file
# GEMINI_API_KEY=your_api_key_here基本用法
# Build the server
npm run build
# Start the server
node dist/mcp-server.jsMCP客户端集成
此MCP服务器可以与各种MCP兼容客户端集成:
- 克劳德桌面 -在Claude设置中添加为MCP服务器
- 光标 -在Cursor的AI/MCP设置中配置
- VS代码 -与MCP兼容的扩展一起使用
有关每个客户端的详细集成说明,请参阅 MCP客户端配置指南 在MCP文档中。
快速客户端设置
使用我们简化的客户端安装命令:
# Install and configure for Claude Desktop
npm run install:claude
# Install and configure for Cursor
npm run install:cursor
# Install and configure for VS Code
npm run install:vscode每个命令都设置了相应的配置文件,并提供了完成集成的说明。
💻 使用示例
对于初学者
直接使用服务器:
- 启动服务器:
node dist/mcp-server.js- 使用提供的测试脚本进行交互:
# Test basic context management
node test-gemini-context.js
# Test caching features
node test-gemini-api-cache.js在Node.js应用程序中使用:
import { GeminiContextServer } from './src/gemini-context-server.js';
async function main() {
// Create server instance
const server = new GeminiContextServer();
// Generate a response in a session
const sessionId = "user-123";
const response = await server.processMessage(sessionId, "What is machine learning?");
console.log("Response:", response);
// Ask a follow-up in the same session (maintains context)
const followUp = await server.processMessage(sessionId, "What are popular algorithms?");
console.log("Follow-up:", followUp);
}
main();对于高级用户
使用自定义配置:
// Custom configuration
const config = {
gemini: {
apiKey: process.env.GEMINI_API_KEY,
model: 'gemini-2.0-pro',
temperature: 0.2,
maxOutputTokens: 1024,
},
server: {
sessionTimeoutMinutes: 30,
maxTokensPerSession: 1000000
}
};
const server = new GeminiContextServer(config);使用缓存系统进行成本优化:
// Create a cache for large system instructions
const cacheName = await server.createCache(
'Technical Support System',
'You are a technical support assistant for a software company...',
7200 // 2 hour TTL
);
// Generate content using the cache
const response = await server.generateWithCache(
cacheName,
'How do I reset my password?'
);
// Clean up when done
await server.deleteCache(cacheName);🔌 使用MCP工具(如光标)
该服务器实现了模型上下文协议(MCP),使其与Cursor或其他人工智能增强的开发环境等工具兼容。
可用的MCP工具
- 上下文管理工具:
- generate_text -根据上下文生成文本 - get_context -获取会话的当前上下文 - clear_context -清晰的会话上下文 - add_context -添加特定上下文条目 - search_context -从语义上查找相关上下文
- 缓存工具:
- mcp_gemini_context_create_cache -为大型上下文创建缓存 - mcp_gemini_context_generate_with_cache -使用缓存的上下文生成 - mcp_gemini_context_list_caches -列出所有可用缓存 - mcp_gemini_context_update_cache_ttl -更新缓存TTL - mcp_gemini_context_delete_cache -删除缓存
与游标连接
与一起使用时 光标,您可以通过MCP配置进行连接:
{
"name": "gemini-context",
"version": "1.0.0",
"description": "Gemini context management and caching MCP server",
"entrypoint": "dist/mcp-server.js",
"capabilities": {
"tools": true
},
"manifestPath": "mcp-manifest.json",
"documentation": "README-MCP.md"
}有关MCP工具的详细使用说明,请参阅 README-MCP.md.
⚙️ 配置选项
环境变量
创建一个 .env 具有以下选项的文件:
# Required
GEMINI_API_KEY=your_api_key_here
GEMINI_MODEL=gemini-2.0-flash
# Optional - Model Settings
GEMINI_TEMPERATURE=0.7
GEMINI_TOP_K=40
GEMINI_TOP_P=0.9
GEMINI_MAX_OUTPUT_TOKENS=2097152
# Optional - Server Settings
MAX_SESSIONS=50
SESSION_TIMEOUT_MINUTES=120
MAX_MESSAGE_LENGTH=1000000
MAX_TOKENS_PER_SESSION=2097152
DEBUG=false🧪 发展
# Build TypeScript files
npm run build
# Run in development mode with auto-reload
npm run dev
# Run tests
npm test📚 进一步阅读
- 有关MCP的具体用法,请参阅 README-MCP.md
- 探索清单 mcp-manifest.json 了解可用工具
- 检查存储库中的示例脚本以了解使用模式
📋 未来改进
- 上下文和缓存的数据库持久性
- 缓存大小管理和驱逐策略
- 基于向量的语义搜索
- 分析和指标跟踪
- 与矢量存储集成
- 用于上下文管理的批处理操作
- 混合缓存策略
- 自动提示优化
📄 许可证
麻省理工学院
