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 documents34This example shows how to use the Kern MCP tools to interact with your Notion workspace.561. Start by setting up a new internal integration in Notion: https://www.notion.so/profile/integrations72. 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".910Dependencies: uv pip install kern-ai mcp openai1112Usage:13 python cookbook/14_tools/mcp/notion_mcp_agent.py14"""1516import asyncio17import json18import os19from textwrap import dedent2021from kern.agent import Agent22from kern.models.openai import OpenAIResponses23from kern.tools.mcp import MCPTools24from mcp import StdioServerParameters252627async 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 )3334 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)4243 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 )5657 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 )636465if __name__ == "__main__":66 asyncio.run(run_agent())