Skip to main content

Overview

Build an autonomous agent that connects to your Notion workspace, pulls data from databases and pages, and generates local analysis files — CSV exports, summary reports, and dashboards.

Prerequisites

Environment Variables

# .env
OPENAPI_MCP_HEADERS='{"Authorization": "Bearer ntn_xxxxxxxxxxxxxxxxxxxx", "Notion-Version": "2022-06-28"}'
ANTHROPIC_API_KEY=your-anthropic-key-here

Installation

# With uv (recommended)
uv pip install upsonic python-dotenv

# With pip
pip install upsonic python-dotenv

Example: Export Project Tracker to CSV

The agent queries a Notion database, extracts project data, and writes a structured CSV and summary report to the workspace.
import os
from dotenv import load_dotenv
from upsonic import AutonomousAgent, Task
from upsonic.tools.mcp import MCPHandler

load_dotenv()

notion_handler = MCPHandler(
    command="npx -y @notionhq/notion-mcp-server",
    env={
        "OPENAPI_MCP_HEADERS": os.getenv("OPENAPI_MCP_HEADERS")
    },
    timeout_seconds=60
)

agent = AutonomousAgent(
    model="anthropic/claude-sonnet-4-6",
    workspace="./exports",
    tools=[notion_handler]
)

task = Task(
    description="""
    Search my Notion workspace for a database called "Project Tracker".
    Query all entries and export the data.

    1. Write a CSV file 'projects.csv' with columns:
       project_name, status, owner, due_date, priority, completion_pct
    2. Write a summary report 'project_summary.md' with:
       - Total projects by status (table)
       - Overdue projects (list with owner and days overdue)
       - Completion distribution (how many at 0-25%, 25-50%, etc.)
       - Top risks and recommendations
    """
)

agent.print_do(task)

Example: Meeting Notes Aggregator

The agent searches for all meeting notes from the past week and creates a consolidated action items report.
import os
from dotenv import load_dotenv
from upsonic import AutonomousAgent, Task
from upsonic.tools.mcp import MCPHandler

load_dotenv()

notion_handler = MCPHandler(
    command="npx -y @notionhq/notion-mcp-server",
    env={
        "OPENAPI_MCP_HEADERS": os.getenv("OPENAPI_MCP_HEADERS")
    },
    timeout_seconds=60
)

agent = AutonomousAgent(
    model="anthropic/claude-sonnet-4-6",
    workspace="./weekly-digest",
    tools=[notion_handler]
)

task = Task(
    description="""
    Search my Notion workspace for all pages under "Meetings" that were
    edited in the last 7 days.

    For each meeting page, extract:
    - Meeting title and date
    - Attendees
    - Action items (with assignee and deadline if available)

    Write two files:
    1. 'weekly_action_items.md' — all action items grouped by assignee,
       sorted by deadline. Include a count per person.
    2. 'meeting_log.csv' — one row per meeting with columns:
       date, title, attendee_count, action_item_count
    """
)

agent.print_do(task)

Available Tools

The Notion MCP server exposes tools including:
ToolDescription
notion_searchSearch pages and databases
notion_query_databaseQuery a database with filters and sorts
notion_create_pageCreate a new page
notion_update_pageUpdate page properties
notion_get_pageRetrieve a page and its content
notion_append_block_childrenAdd content blocks to a page

Security Notes

  • Create a dedicated Notion integration for your agent — don’t reuse personal tokens.
  • Only share the specific pages and databases your agent needs access to via the integration settings.
  • Store your integration token in .env — never hardcode it.