关于 Gemini Context MCP Server
Gemini Context MCP Server 是一个面向 Cursor 等 MCP 客户端的服务,利用 Gemini 较大的上下文窗口增强 AI 工具的能力。它提供会话上下文管理和大型提示词的缓存功能。
主要功能
- 01支持最高 2M token 的上下文窗口
- 02基于会话的上下文添加、检索与搜索
- 03语义搜索相关上下文
- 04大型提示词缓存与 TTL 管理
- 05过期会话和缓存的自动清理
适用场景
- 01在 Cursor 中复用大型系统提示词以降低 token 成本
- 02为长时间多轮对话维护上下文状态
- 03管理和更新多个缓存的上下文内容
MCP 相关
该项目是一个 MCP 服务,向 MCP 客户端提供 generate_text、search_context 等上下文管理工具,以及缓存创建、列出、更新和删除等工具。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
Gemini Context MCP 服务
这是一个功能强大的 MCP(Model Context Protocol)服务实现,利用 Gemini 的能力进行上下文管理和缓存。此服务充分利用 Gemini 的 2M token 上下文窗口,并提供高效的大型上下文缓存工具。
🚀 功能
上下文管理
- 支持高达 2M token 的上下文窗口 - 利用 Gemini 强大的上下文能力
- 基于会话的对话 - 在多次交互中保持对话状态
- 智能上下文跟踪 - 使用元数据添加、检索和搜索上下文
- 语义搜索 - 使用语义相似度查找相关上下文
- 自动清理上下文 - 会话和上下文会自动过期
API 缓存
- 大型提示词缓存 - 高效重复使用大型系统提示词和指令
- 成本优化 - 降低频繁使用的上下文所产生的 token 成本
- 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.js
MCP 客户端集成
此 MCP 服务可以集成到各种兼容 MCP 的客户端中:
- Claude Desktop - 在 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
每条命令都会设置相应的配置文件,并提供完成集成的说明。
💻 用法示例
入门指南
直接使用服务:
-
启动服务:
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 工具使用(如 Cursor)
此服务实现了 Model Context Protocol(MCP),因此兼容 Cursor 等工具以及其他 AI 增强型开发环境。
可用的 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- 更新缓存 TTLmcp_gemini_context_delete_cache- 删除缓存
连接 Cursor
搭配 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
📚 延伸阅读
- MCP 专用用法请参阅 README-MCP.md
- 查看 mcp-manifest.json 中的清单,了解可用工具
- 查看仓库中的示例脚本,了解用法模式
📋 后续改进
- 为上下文和缓存提供数据库持久化
- 缓存大小管理和淘汰策略
- 基于向量的语义搜索
- 分析和指标跟踪
- 与向量存储集成
- 上下文管理的批量操作
- 混合缓存策略
- 自动优化提示词
📄 许可证
MIT
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。