首页
学习
活动
专区
圈层
工具
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
首页
学习
活动
专区
圈层
工具
MCP广场
MCP广场 >详情页
gemini-context-mcp-server2025-05-210分享
github
一种MCP服务器实现,最大化利用Gemini的2M令牌上下文窗口,并提供跨多个AI客户端应用程序的高效上下文管理和缓存工具。
By ogoldberg
2025-05-210
github
详情内容

Gemini Context MCP 服务器

一个强大的MCP(模型上下文协议)服务器实现,利用Gemini的功能进行上下文管理和缓存。该服务器充分利用了Gemini的2M令牌上下文窗口的价值,同时提供了高效缓存大量上下文的工具。

🚀 特性

上下文管理

  • 支持高达2M令牌的上下文窗口 - 利用Gemini广泛的上下文能力
  • 基于会话的对话 - 在多次交互中保持对话状态
  • 智能上下文跟踪 - 带有元数据添加、检索和搜索上下文
  • 语义搜索 - 使用语义相似度查找相关上下文
  • 自动上下文清理 - 会话和上下文自动过期

API 缓存

  • 大提示缓存 - 高效重用大型系统提示和指令
  • 成本优化 - 减少频繁使用上下文的令牌使用成本
  • TTL 管理 - 控制缓存到期时间
  • 自动清理 - 自动移除已过期的缓存

🏁 快速开始

前提条件

安装

# 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

每个命令都会设置适当的配置文件,并提供完成集成的说明。

💻 使用示例

对于初学者

直接使用服务器:

  1. 启动服务器:

    node dist/mcp-server.js
    
  2. 使用提供的测试脚本进行交互:

    # 测试基本上下文管理
    node test-gemini-context.js
    
    # 测试缓存功能
    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或其他增强型开发环境等工具兼容。

可用的MCP工具

  1. 上下文管理工具:

    • generate_text - 生成带有上下文的文本
    • get_context - 获取会话的当前上下文
    • clear_context - 清除会话上下文
    • add_context - 添加特定的上下文条目
    • search_context - 语义上查找相关上下文
  2. 缓存工具:

    • 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 连接

当与 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

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档