pi-cursor-sdk

repository·main·Indexed 42 days ago

https://github.com/fitchmultz/pi-cursor-sdk

A pi provider extension backed by @cursor/sdk local and cloud agents that enables the use of Cursor models within the pi environment. It integrates pi-native features like model selection and session management while preserving the Cursor SDK agent loop. The SDK supports various model families including Grok 4.5, Claude Opus 5, Claude Fable 5, and GPT-5.6 (Sol, Terra, Luna), each with specific tool dialects and system prompt guidance.

Tokens
54.1K
Snippets
145
Records
242
Agent score
54%

What's inside pi-cursor-sdk

  1. Understand Plan-mode Guardrails

    main

    In SDK-native Plan mode, most models (except Grok 4.5) receive an additional <plan_mode_guardrails> block. This block enforces the following constraints:

    • Only edit markdown files while in plan mode.
    • If the user is refining a plan, stay in plan mode and keep edits in markdown.
    • To switch to agent mode for non-markdown edits (building, implementing, or writing code), you must call SwitchMode with target_mode_id=agent.
    <plan_mode_guardrails>
    - In plan mode, only edit markdown files.
    - If the user is refining the plan, stay in plan mode and keep edits in markdown.
    - If the user explicitly asks you to build, implement, or write the code now, switch to agent mode before making non-markdown edits by calling `SwitchMode` with `target_mode_id=agent`.
    </plan_mode_guardrails>
  2. Understand Cursor's model-specific prompt tuning

    main

    Cursor architecture involves tuning instructions and tools specifically for each frontier model. While Cursor's official documentation states that the agent loop, tool schema, prompts, and stream shape remain constant across models, empirical evidence shows that persisted system-message text differs by model family. Additionally, different models may report different tool-name dialects.

    When working with the SDK, be aware that while the high-level interface might appear constant, the underlying system guidance and tool surface can vary significantly depending on whether you are using models like Grok 4.5, Claude Opus 5, or the GPT-5.6 tiers (Sol, Terra, and Luna).

  3. Map Pi thinking levels to Cursor SDK parameters

    main

    When using the Cursor SDK, you must map Pi's native thinking levels to the specific values expected by the Cursor models.

    Pi Thinking Levels: off, minimal, low, medium, high, xhigh, max.

    Mapping Rules:

    • off $\rightarrow$ none (if supported), else off, else false.
    • minimal $\rightarrow$ minimal.
    • low $\rightarrow$ low.
    • medium $\rightarrow$ medium.
    • high $\rightarrow$ high (or true for boolean-only thinking models).
    • xhigh $\rightarrow$ xhigh (or extra-high).
    • max $\rightarrow$ max.

    Important Implementation Details:

    • Use null for unsupported Pi levels so the system can skip or clamp them natively.
    • For models that only support boolean thinking (true/false), use an explicit mapping where unsupported levels are null to avoid accidental enablement.
    • The thinkingLevelMap only controls Pi-native behavior; the actual ModelSelection.params for Agent.create() must be constructed using the active Pi level and discovered Cursor metadata.
    // Example shape for boolean-only thinking models
    {
      off: "false",
      minimal: null,
      low: null,
      medium: null,
      high: "true",
      xhigh: null,
      max: null,
    }
  4. Understand the three Cursor tool surfaces in pi

    main

    When running pi-cursor-sdk, there are three distinct tool namespaces available to the Cursor model. It is important to distinguish between them to understand what the model can call and what you see in the UI.

    1. Cursor SDK host tools: Owned by the Cursor local agent. These are native tools like read, bash, etc. They are always available to the model.
    2. Configured Cursor MCP: Tools loaded from your Cursor settings (e.g., ~/.cursor/mcp.json). These appear as neutral Cursor MCP activity cards.
    3. Pi bridge (pi__*): Owned by the pi-cursor-sdk loopback MCP. This exposes real pi tool names like pi__cursor_ask_question or pi__cursor_activate_skill.

    Note: Pi CLI tool toggles (like --no-tools) only affect the Pi bridge; they do not disable Cursor SDK host tools or configured Cursor MCP servers.

  5. Current implementation status: Local resume is opt-in

    main

    As of the current decision (Route A), the pi-cursor-sdk maintains local resume as opt-in (default-off).

    Reasoning:

    • The current implementation avoids the 'tree custom-entry hazard' where a resumed agent could potentially access 'future tokens' (data from a later part of the conversation tree).
    • Default-off configuration and existing tests remain intact.
    • Same-session restarts are currently platform-proven and safe.
  6. Verify Cursor provider and runtime behaviors

    main

    When debugging the Cursor provider or runtime, ensure the following surfaces are explicitly validated, as mocks may not fully capture real-world behavior:

    • Tool Aborts: Cleanup behavior when a long-running bridged tool is aborted or cancelled.
    • Native Replay Cards: Rendering of shell, edit, and write cards when renderers change.
    • Bridge UI: The cursor_ask_question UI when the protocol changes.
    • MCP Timeouts: Behavior of callTool (default 3600s), initialize/listTools (default 10s), and SDK-default unknown protocol stacks.
    • Activity Replay: Formatting for semSearch and recordScreen activity replays.
    • Ambient Settings: How Cursor handles startup filtering or local settings changes.
    • Model Discovery: Behavior of aliases and context variants when model-discovery code or SDK versions change.
  7. Understand SDK reporting and visibility limits

    main

    There are several integration boundaries in the pi-cursor-sdk that affect how tools are displayed:

    Tool Visibility Limits

    • Live Web-Search Ordering: Local Cursor WebSearch might not appear in live streaming events (onDelta, onStep, run.stream()). The web search card may only appear after run.wait() finishes to avoid buffering the entire assistant response.
    • WebFetch Availability: The SDK can only display a Cursor web fetch if the SDK reports a webFetchToolCall, a web-fetch-shaped MCP completion, or a web-fetch host alias. It cannot force Cursor to expose WebFetch if it isn't in the tool set.
    • Future SDK Tools: If Cursor introduces new tools with unknown names or payloads, the SDK falls back to neutral Cursor activity cards with scrubbed text.

    Error Handling

    • Process-level Exceptions: Raw AbortError DOMExceptions and certain WriteIterableClosedError events are suppressed during active provider turns to maintain a stable session lifecycle. However, fatal errors like write EPIPE (in specific multi-frame synchronous write paths) remain fatal.
  8. Understand the Platform Smoke Target Matrix

    main

    The platform smoke suite executes across three distinct target environments to ensure cross-platform stability. Each target uses a specific Crabbox provider and follows a specific execution contract.

    TargetCrabbox ProviderExecution EnvironmentTUI Visual Contract
    macosssh static localhostNative macOS shellPTY ANSI capture + host-side render
    ubuntulocal-containerDocker Ubuntu containerPTY ANSI capture + host-side render
    windows-nativeparallelsWindows 11 clone (PowerShell/Node)ConPTY ANSI capture + host-side render

    Key Architectural Concepts:

    • Host-side Rendering: To avoid browser dependency drift inside test targets, targets capture the raw ANSI stream, and the host machine performs the rendering (e.g., via xterm/Playwright) to generate visual evidence.
    • Fail-Fast Logic: The gate runs targets sequentially. If one target fails, the suite stops immediately to prevent wasting Cursor API tokens and resources.
  9. Understand the limitations of Native Replay

    main

    Native replay is display-only. It is a mechanism to show what happened in Cursor, not to execute actions in pi.

    What replay does NOT do:

    • No Execution: It does not re-run Cursor-side commands, apply edits/deletes, or call Cursor-side MCP servers.
    • No State Updates: Replay-only cards do not update pi state, generate images, or drive pi workflow controls (like SwitchMode, plan mode, or todo state).
    • No Tool Exposure: Replay does not expose pi tool schemas to Cursor. The local pi MCP bridge is the correct path for exposing active pi tools.
    • No Web Tools: Cursor web search and Cursor web fetch cards are mirrors of SDK-reported activity; pi does not expose executable web search/fetch tools through replay.

    Handling Incomplete Tasks:

    • Started tool calls that never complete are surfaced as neutral Cursor [tool name] did not complete activity cards.
    • Reasons for incompletion include missing completion, aborted, or SDK run failed.
  10. Configure Claude thinking and effort

    main

    Some Claude models support both a thinking boolean and an effort level. To maintain a consistent mental model, Pi treats these as a single dimension:

    1. Pi off: Sends thinking=false to the Cursor SDK.
    2. Pi enabled levels: Sends thinking=true AND the mapped effort value (e.g., low, medium, high).

    Users can change the effort level using shift+tab in the UI. There is no separate toggle for thinking because it is tied directly to the effort level.

  11. Understand the Cursor provider tool contract

    main

    When running pi-cursor-sdk locally, there are two distinct tool surfaces interacting with Cursor:

    1. Cursor-native surface: Includes Cursor local-agent tools, settings, plugins, and configured Cursor MCP servers. This surface is owned by the Cursor SDK local agent path and is not affected by Pi CLI tool toggles like --no-tools or --exclude-tools.
    2. pi bridge surface: Exposes active pi tools through a local loopback MCP bridge. This surface is affected by Pi CLI tool toggles because they change the active pi tool registry.

    Key Bridge Behaviors:

    • Naming: Bridged tools are exposed to Cursor with collision-safe names using the pi__ prefix (e.g., pi__sem_reindex).
    • Tool Preference: When the bridge is active, Cursor is instructed to prefer pi__mcp for MCP work and pi__subagent for delegation. Cursor-configured MCP and native subagents act as fallbacks.
    • Execution: The bridge queues Cursor's MCP calls, emits a toolCall in pi, waits for the toolResult, and resolves it back to the Cursor SDK run without creating a new Agent.
    • Built-in Tools: Overlapping tools like read, bash, write, edit, grep, find, and ls are hidden by default to avoid conflict with Cursor's native capabilities. Use PI_CURSOR_EXPOSE_BUILTIN_TOOLS=1 to expose them.