buildRun gave every Run a new random AgentRunRequest.conversation_id, so Cursor's backend sent each turn to whichever machine and the prompt cache hit only by chance (congee949: cache_read 0, 1152, 0, 0, 0, 0 over six turns of one Grok conversation). The id is now made as the built-in's cursorConversation makes it: a UUID hashed from what names the session — the request's prompt_cache_key (Codex's thread id), else the session magpie (or OpenCode) tells the new chat.headers hook, which the hook hands the loader's fetch in x-magpie-cursor-session and which goes no further — together with the conversation's first user message, so subagents under one session are conversations of their own. With nothing naming the session it stays random per Run: magpie's own "magpie-<24 hex>" session, made of the first message alone when the agent names none, is taken as none, as the built-in's nativeSessionOf does. The hook only marks requests to cursor, as every plugin's chat.headers sees every provider's. TurnEndedUpdate.input_tokens counts the whole prompt, the cache read and written included; it was taken as the uncached input and the cache added again for prompt_tokens. Now, as cursorUsage: input = max(input_tokens - cache_read - cache_write, 0), reasoning_tokens (field 5) given as completion_tokens_details.reasoning_tokens. The prompt is guessed only when Cursor counted nothing at all, not when it was all read from the cache. conversation.test.mjs runs the plugin against a fake Cursor (its API a stand-in fetch, the agent API an HTTP/2 server on 127.0.0.1, any other connection refused): a session's turns keep one id, another session or another first message gets its own, prompt_cache_key wins over the host's session, no session gives a new id each Run, the hook leaves other providers alone, and the issue's numbers come out counted once. All five fail against 0.1.5.
4.1 KiB
@magpie-community/opencode-cursor-auth
Signs in to a Cursor subscription (Pro, Pro+, Ultra,
Teams) and makes its requests on the API cursor-agent talks to, in
OpenCode and in magpie. Provider id: cursor.
Signing in
An account is a Cursor access token. There are three ways to get one:
- Cursor (browser): the sign-in
cursor-agent logindoes. The pagecursor.com/loginDeepControlis opened with a PKCE challenge, and the plugin pollsapi2.cursor.sh/auth/polluntil the browser has signed in. The account is named by its email (DashboardService/GetMe). - cursor-agent's sign-in: takes the account
cursor-agent loginsigned in to. Its token is read on every request from where the CLI keeps it: the macOS keychain (cursor-access-token), else itsauth.json(~/.cursor/,$XDG_CONFIG_HOME/cursor/or%APPDATA%\Cursor\). The plugin never writes it; when it has less than 5 minutes left,cursor-agent statusis run, which renews it. - Cursor API key: a key from cursor.com/dashboard → Integrations. It is
exchanged for a token at
/auth/exchange_user_api_key, as the CLI does withCURSOR_API_KEY, and exchanged again when the token runs out.
Sign-ins are kept where OpenCode keeps them (auth.json; in magpie,
plugin-auth.json). A browser sign-in's token lasts about two months.
Cursor gives the CLI no way to renew it, so after that you sign in again.
Requests
OpenCode speaks chat completions (@ai-sdk/openai-compatible); the
plugin's fetch answers them on Cursor's agent API:
- The agent API is the one
ServerConfigService/GetServerConfignames for the account (a team may be served in one region only), elseagentn.global.api5.cursor.sh. A refusal that names a region is tried once more with the config asked again. - Each request is one
agent.v1.AgentService/Run, a Connect stream both ways over HTTP/2, with the CLI's headers (x-cursor-client-type: cli, its version, privacy mode on). - The whole conversation goes each time, as AI SDK messages kept as blobs
the server asks for. The caller's tools are MCP tools, listed in the
system prompt; the model calls them through Cursor's
CallDynamicTool, and the calls come back as chat completion tool calls. Cursor's own tools (shell, file reads, edits) are never run. - A conversation's Runs share one
conversation_id, so Cursor sends them to the machine that has the prompt cached (Grok on Cursor caches by machine). It is made from what names the session — the request'sprompt_cache_key(Codex's thread id), else the session thechat.headershook is told — and the conversation's first user message, so subagents under one session are conversations of their own. With nothing naming the session, each Run has a new one. - Text, thinking (
reasoning_content) and token usage come back streamed or not. Cursor'sinput_tokenscounts the cached prompt too: the cache read and written is taken out of it, and given ascached_tokensandcache_write_tokens, so it is counted once; its reasoning tokens arereasoning_tokens. Failures keep their statuses: 401 to sign in again, 429 at the usage limit, 400 for a prompt too long, 403 for a region refusal.
Models
The config hook declares auto (Cursor's pick). Once signed in, the
provider.models hook lists the account's models
(AgentService/GetUsableModels, cached 10 minutes), by the ids
cursor-agent models shows:
- Cursor lists a model once per effort (
gpt-5.6-low,gpt-5.6-high, …). Each family is one model here, with the efforts as variants; the request'sreasoning_effortpicks the id, fitted to the nearest one there is.-fastand-thinkingmodels stay models of their own; aservice_tierofprioritypicks the fast one. - The context window is 1M for a model whose name says 1M, else 200K.
Not included
- Usage and plan limits.
- Web search: Cursor's own web search is refused like its other tools.
- Switching between several accounts. OpenCode keeps one sign-in per provider.
- The Responses and Messages APIs: only chat completions are answered.