> ## Documentation Index
> Fetch the complete documentation index at: https://ikun.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Notion MCP 代理

使用 [Notion MCP 服务器](https://github.com/makenotion/notion-mcp-server) 创建一个可以创建、更新和搜索 Notion 页面的代理：

```python theme={null}
"""
Notion MCP Agent - 管理您的文档

本示例展示了如何使用 Agno MCP 工具与您的 Notion 工作区进行交互。

1. 首先在 Notion 中设置一个新的内部集成：https://www.notion.so/profile/integrations
2. 导出您的新 Notion 密钥：`export NOTION_API_KEY=ntn_****`
3. 将相关的 Notion 页面连接到该集成。为此，您需要访问该页面，点击三个点，然后选择“连接到集成”。

依赖项：pip install agno mcp openai

用法：
  python cookbook/tools/mcp/notion_mcp_agent.py
"""

import asyncio
import json
import os
from textwrap import dedent

from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.mcp import MCPTools
from mcp import StdioServerParameters


async def run_agent():
    token = os.getenv("NOTION_API_KEY")
    if not token:
        raise ValueError(
            "缺少 Notion API 密钥：请提供 --NOTION_API_KEY 或设置 NOTION_API_KEY 环境变量"
        )

    command = "npx"
    args = ["-y", "@notionhq/notion-mcp-server"]
    env = {
        "OPENAPI_MCP_HEADERS": json.dumps(
            {"Authorization": f"Bearer {token}", "Notion-Version": "2022-06-28"}
        )
    }
    server_params = StdioServerParameters(command=command, args=args, env=env)

    async with MCPTools(server_params=server_params) as mcp_tools:
        agent = Agent(
            name="NotionDocsAgent",
            model=OpenAIChat(id="gpt-4o"),
            tools=[mcp_tools],
            description="通过 MCP 查询和修改 Notion 文档的代理",
            instructions=dedent("""\
                您可以通过 MCP 工具访问 Notion 文档。
                - 使用工具读取、搜索或更新页面。
                - 在进行修改前与用户确认。
            """),
            markdown=True,
            show_tool_calls=True,
        )

        await agent.acli_app(
            message="您是一个可以访问 Notion 工作区和页面的有帮助的助手。",
            stream=True,
            markdown=True,
            exit_on=["exit", "quit"],
        )


if __name__ == "__main__":
    asyncio.run(run_agent())
```
