Gemini Context MCP 服务器
一个强大的 MCP(模型上下文协议)服务器实现,充分利用了 Gemini 的上下文管理和缓存功能。该服务器最大限度地发挥了 Gemini 2M 令牌上下文窗口的价值,同时提供了高效缓存大型上下文的工具。
🚀 功能
上下文管理
API缓存
大型提示缓存- 高效重用大型系统提示和指令
成本优化——降低常用上下文的令牌使用成本
TTL 管理- 控制缓存过期时间
自动清理- 自动删除过期的缓存
Related MCP server: MCP Gemini Server
🏁 快速入门
先决条件
已安装 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.js
MCP 客户端集成
该 MCP 服务器可以与各种兼容 MCP 的客户端集成:
Claude 桌面- 在 Claude 设置中添加为 MCP 服务器
Cursor - 在 Cursor 的 AI/MCP 设置中配置
VS Code - 与 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
每个命令都会设置适当的配置文件并提供完成集成的说明。
💻 使用示例
对于初学者
直接使用服务器:
启动服务器:
使用提供的测试脚本进行交互:
# 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 工具(如 Cursor)一起使用
该服务器实现了模型上下文协议 (MCP),使其与 Cursor 或其他 AI 增强开发环境等工具兼容。
可用的 MCP 工具
上下文管理工具:
缓存工具:
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 - 删除缓存
使用光标连接
与Cursor一起使用时,您可以通过 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
📚 进一步阅读
📋 未来的改进
上下文和缓存的数据库持久性
缓存大小管理和驱逐策略
基于向量的语义搜索
分析和指标跟踪
与向量存储集成
上下文管理的批量操作
混合缓存策略
自动提示优化
📄 许可证
麻省理工学院