上下文解析引擎

一个AI就绪的上下文中间件,提供自然语言查询解析、Google日历集成和智能时段建议功能。

  • by rishabh23rohil
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 rishabh23rohil/mcp-context-engine

使用说明

MCP上下文引擎

AI就绪的上下文中间件 具有自然语言查询解析、谷歌日历集成和智能插槽建议功能。基于FastAPI构建,实现基于NLP的意图分类和实时可用性分析。


建筑

flowchart LR
    subgraph Client[Client Layer]
        Query[Natural Language Query]
    end
    
    subgraph Engine[MCP Context Engine]
        NLP[NLP Parser]
        Intent[Intent Classifier]
        Providers[Data Providers]
        Avail[Availability Engine]
    end
    
    subgraph Sources[External Sources]
        GCal[Google Calendar]
        GitHub[GitHub API]
        Notion[Notion API]
    end
    
    Query --> NLP
    NLP --> Intent
    Intent --> Providers
    Providers --> GCal
    Providers --> GitHub
    Providers --> Notion
    GCal --> Avail
    GitHub --> Avail
    Notion --> Avail
    Avail --> Response[AI-Ready Context]

请求/响应流

sequenceDiagram
    participant U as User
    participant API as FastAPI
    participant NLP as NLP Engine
    participant Cal as Calendar Provider
    participant Avail as Availability Engine

    U->>API: POST /query "am I free tomorrow at 3pm?"
    API->>NLP: Parse natural language
    NLP->>Cal: Fetch events for date range
    Cal-->>NLP: Return events
    NLP->>Avail: Check conflicts
    Avail-->>API: availability: busy/free
    API-->>U: JSON response with context

技能展示

技能 实施
API设计 带有自动生成OpenAPI文档的RESTful FastAPI
自然语言处理 具有日部分识别功能的自定义时间表达式解析器
外部API集成 Google日历ICS、GitHub API、Notion API提供商
测试 具有边缘案例覆盖的pytest(24小时解析,时区)
开发运维 带有GitHub Actions的CI/CD管道,Docker就绪
整洁架构 模块化提供商、路由器和核心业务逻辑

技术栈

组件 技术
框架 FastAPI+Uvicorn
HTTP客户端 httpx(异步)
日历解析 icalendar
配置 复制设置
时区处理 tzdata+python-dateutil
测试 pytest
CI/CD GitHub操作

项目结构

src/app/
├── core/                   # Business logic
│   ├── config.py           # Environment settings
│   ├── nlp.py              # Natural language parsing
│   ├── timeparse.py        # Time expression handling
│   ├── availability.py     # Conflict detection engine
│   └── intent.py           # Query intent classification
├── providers/              # External data sources
│   ├── calendar_ics.py     # Google Calendar ICS
│   ├── github.py           # GitHub API provider
│   └── notion.py           # Notion API provider
├── routers/                # API endpoints
│   ├── query.py            # Main context endpoint
│   └── debug.py            # Diagnostics routes
└── main.py                 # FastAPI entrypoint

截图

Swagger用户界面

用于查询日历上下文的交互式API文档。

可用性检查

查询: am I free tomorrow at 03:10?

插槽建议

查询: any slot tomorrow morning for 45 min


快速开始

先决条件

  • Python 3.11+
  • 具有ICS访问权限的谷歌日历

安装

# Clone repository
git clone https://github.com/rishabh23rohil/mcp-context-engine.git
cd mcp-context-engine

# Create virtual environment
python -m venv .venv
source .venv/bin/activate  # Windows: .venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Configure environment
cp .env.example .env
# Edit .env with your Google Calendar ICS URL

跑

uvicorn src.app.main:app --reload --port 8000

打开 http://127.0.0.1:8000/docs


API终点

端点 方法 描述
/healthz GET 活体检测
/version GET 构建版本
/debug/providers GET 列出活动提供者
/debug/calendar?limit=5 GET 预览即将举行的活动
/debug/calendar/diag GET ICS获取诊断
/query 职位 主上下文引擎

查询示例

查询 预期响应
am I free tomorrow at 11:30? availability: free
am I free tomorrow at 03:10? availability: busy (有冲突)
any slot tomorrow morning for 45 min suggested_slots: [...]
book 30 min after 15:00 today suggested_slots 从15:00开始

响应示例

{
  "availability": "busy",
  "conflicts": [
    {"title": "Team Meeting", "start": "2026-02-02T09:00:00-06:00", "end": "2026-02-02T10:00:00-06:00"}
  ],
  "explanation": "Conflicts with Team Meeting 09:00-10:00.",
  "suggested_slots": [
    {"start": "2026-02-02T10:00:00-06:00", "end": "2026-02-02T10:45:00-06:00", "reason": "earliest free segment"}
  ]
}

测试

pytest tests -q

测试覆盖范围包括:

  • 24小时时间解析
  • 日间识别(上午/下午/晚上)
  • 包容性/排他性边缘政策
  • 全天事件正常化
  • 工作时间段生成

配置

变量 描述 默认值
APP_ENV 环境(本地/产品) local
DEFAULT_TZ 时区 America/Chicago
WORK_HOURS_START 工作日开始 09:00
WORK_HOURS_END 工作日结束 18:00
CALENDAR_ICS_URL 谷歌日历ICS URL 必填
GITHUB_TOKEN GitHub API令牌 可选
NOTION_TOKEN 通知API令牌 可选

路线图

  • [x] M0:FastAPI脚手架、配置、日志
  • [x] M1:健康和诊断路线
  • [x] M2:谷歌日历ICS提供商
  • [x] M3:自然语言可用性引擎
  • []M4:GitHub问题/PR集成
  • []M5:Notion数据库提供程序

作者

里沙布·罗希尔


许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。