Notion API客户端

一个用于与Notion API交互的Model Context Protocol (MCP)客户端,支持异步操作、并发控制和环境配置,适用于现代AI工具如Claude。

  • by phoenine
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 phoenine/Notion-MCP

使用说明

MCP客户端概念

用于与Notion API交互的模型上下文协议(MCP)客户端。该客户端旨在用于支持MCP扩展功能的现代AI工具,如Claude。

特性

  • 具有重试逻辑的异步API操作
  • 遵守Notion API费率限制的并发控制
  • 基于环境的配置
  • 基于JSON的操作配置
  • 支持MCP的灵活服务器实现
  • 当MCP不可用时,回退到标准HTTP API

安装

pip install notion-mcp

或者从源代码安装:

git clone https://github.com/yourusername/notion-mcp.git
cd notion-mcp
pip install -e .

对于MCP支持,请额外安装MCP:

pip install notion-mcp[mcp]

配置

创建一个 .env 使用您的Notion API凭据和配置文件:

NOTION_TOKEN=your_notion_token_here
ROOT_PAGE_ID=your_root_page_id_here
MAX_RETRIES=3
RETRY_DELAYS=1,3,5
CONCURRENCY_LIMIT=5
TIMEOUT_MS=30000

用法

命令行

使用默认配置运行:

notion-mcp

使用自定义JSON配置文件运行:

notion-mcp config.json

服务器

启动服务器(如果有MCP支持):

notion-mcp-server --host 0.0.0.0 --port 8000

如果可用,服务器将尝试使用MCP协议。如果没有,它将退回到接受POST请求的标准HTTP服务器 /notion.

HTTP API

使用HTTP API时,将POST请求发送到 /notion 使用JSON正文:

{
  "root_page_id": "your_page_id_here",
  "database_filter": ["Articles", "Resources"],
  "operation": {
    "type": "fill_web_url",
    "params": {
      "web_field": "网址"
    }
  }
}

使用curl的示例:

curl -X POST http://localhost:8000/notion \
  -H "Content-Type: application/json" \
  -d '{"root_page_id": "your_page_id_here", "operation": {"type": "fill_web_url"}}'

JSON配置

创建JSON配置文件以自定义行为:

{
  "root_page_id": "your_page_id_here",
  "database_filter": ["Articles", "Resources"],
  "operation": {
    "type": "fill_web_url",
    "params": {
      "web_field": "网址"
    }
  }
}

与克劳德一起使用

要将此MCP客户端与Claude一起使用,您需要设置服务器,然后配置Claude使用它。以下是一个示例:

{
  "mcp_configurations": [
    {
      "name": "notion",
      "endpoint": "http://localhost:8000/notion",
      "description": "Access and manipulate Notion databases and pages"
    }
  ]
}

然后,在与Claude的对话中,您可以要求它执行以下Notion操作:

Claude, please use the notion MCP to fill the web URL field in all pages of my Articles database.

与其他框架的集成

这 NotionRequestHandler 类提供了一种与不同框架集成的灵活方式:

from notion_mcp.handler import notion_handler

# Create an adapter for your framework
async def my_framework_handler(request):
    # Extract data from your framework's request object
    input_data = await request.json()
    
    # Process with Notion handler
    result = await notion_handler.handle_request(input_data)
    
    # Return in your framework's response format
    return MyFrameworkResponse(json=result)

可用操作

  • fill_web_url:在页面中查找URL字段并将值复制到指定的目标URL字段
  • 更多行动即将到来!

发展

为该项目做出贡献:

  1. 分叉存储库
  2. 创建虚拟环境: python -m venv venv
  3. 激活环境: source venv/bin/activate (Linux/Mac)或 venv\Scripts\activate (Windows)
  4. 安装开发依赖项: pip install -e ".[dev]"
  5. 进行更改
  6. 运行测试: pytest
  7. 提交拉取请求

许可证

麻省理工学院