Notion MCP agent

Using the Notion MCP server to create an Agent that can create, update and search for Notion pages:

1"""
2Notion MCP Agent - Manages your documents
3
4This example shows how to use the Kern MCP tools to interact with your Notion workspace.
5
61. Start by setting up a new internal integration in Notion: https://www.notion.so/profile/integrations
72. Export your new Notion key: `export NOTION_API_KEY=ntn_****`
83. Connect your relevant Notion pages to the integration. To do this, you'll need to visit that page, and click on the 3 dots, and select "Connect to integration".
9
10Dependencies: uv pip install kern-ai mcp openai
11
12Usage:
13 python cookbook/14_tools/mcp/notion_mcp_agent.py
14"""
15
16import asyncio
17import json
18import os
19from textwrap import dedent
20
21from kern.agent import Agent
22from kern.models.openai import OpenAIResponses
23from kern.tools.mcp import MCPTools
24from mcp import StdioServerParameters
25
26
27async def run_agent():
28 token = os.getenv("NOTION_API_KEY")
29 if not token:
30 raise ValueError(
31 "Missing Notion API key: provide --NOTION_API_KEY or set NOTION_API_KEY environment variable"
32 )
33
34 command = "npx"
35 args = ["-y", "@notionhq/notion-mcp-server"]
36 env = {
37 "OPENAPI_MCP_HEADERS": json.dumps(
38 {"Authorization": f"Bearer {token}", "Notion-Version": "2022-06-28"}
39 )
40 }
41 server_params = StdioServerParameters(command=command, args=args, env=env)
42
43 async with MCPTools(server_params=server_params) as mcp_tools:
44 agent = Agent(
45 name="NotionDocsAgent",
46 model=OpenAIResponses(id="gpt-5.2"),
47 tools=[mcp_tools],
48 description="Agent to query and modify Notion docs via MCP",
49 instructions=dedent("""\
50 You have access to Notion documents through MCP tools.
51 - Use tools to read, search, or update pages.
52 - Confirm with the user before making modifications.
53 """),
54 markdown=True,
55 )
56
57 await agent.acli_app(
58 input="You are a helpful assistant that can access Notion workspaces and pages.",
59 stream=True,
60 markdown=True,
61 exit_on=["exit", "quit"],
62 )
63
64
65if __name__ == "__main__":
66 asyncio.run(run_agent())