Skip to content

CTX7-4100: Return an MCP task from the code tools while a code index is built - #3944

Merged
fahreddinozcan merged 10 commits into
masterfrom
CTX7-4100/mcp-code-tool-tasks
Nov 27, 2026
Merged

fahreddinozcan merged 10 commits into
masterfrom
CTX7-4100/mcp-code-tool-tasks

Conversation

@fahreddinozcan

@fahreddinozcan fahreddinozcan commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

While a library's source is being indexed, the code tools answer with an MCP task for clients that declare the Tasks extension, so the client waits for the index and the agent gets the real result.

  • A tool call from a 2026-07-28 request that declares io.modelcontextprotocol/tasks asks /v2/code for a task (task=1); a 202 that names one becomes a resultType: "task" answer. Every other answer and every other client stays as before.
  • The Context7 API keeps the task (upstash/context7app#1870): tasks/get polls /v2/code/tasks/{id} with the poll's own credential, tasks/cancel deletes it. The server keeps no state and needs no new setup; an API without tasks names none, and the call answers with text.
  • Timeouts, 5xx and a busy code store keep a task waiting; index_pending ends it with the API's text (not an error, for tasks and plain calls alike); after 30 minutes without an index it ends with the usual "being indexed" text.
  • tasks/* answer -32021 without the capability and -32602 for an unknown task.
  • Not in scope: get-upgrade-guide and diff-code.

Closes CTX7-4100

@vercel

vercel Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
context7-admin-mcp Ignored Ignored Nov 27, 2026 2:22pm UTC

Request Review

@linear-code

linear-code Bot commented Nov 25, 2026

Copy link
Copy Markdown

CTX7-4100

@fahreddinozcan
fahreddinozcan force-pushed the CTX7-4100/mcp-code-tool-tasks branch from f773b33 to cef3d14 Compare November 27, 2026 09:37
context7-ops[bot]
context7-ops Bot previously requested changes Nov 27, 2026

@context7-ops context7-ops Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To fix on commit cef3d14 before this PR is ready:

  • [major] Public docs still say the code tools never make the agent wait; the new self-hosting env vars are not documented (docs/howto/experimental-tools.mdx:161)
  • [minor] checkGate skips the version normalisation fetchLibraryCode does, so one library version can get several gates (packages/mcp/src/lib/code-task.ts:500)
  • [minor] A transient failure on the poll after the 30-minute wait ends the task with an error, not the promised 'being indexed' text (packages/mcp/src/lib/code-tools.ts:655)
  • [minor] Results delivered through tasks/get are not recorded in tool-call telemetry (packages/mcp/src/lib/code-tools.ts:652)
  • [minor] "Unknown task" lookup-and-throw repeated in three handlers (packages/mcp/src/lib/code-tools.ts:646)
  • [minor] Task lifecycle logic is split between code-task.ts and code-tools.ts (packages/mcp/src/lib/code-tools.ts:636)
  • [minor] MemoryCodeTaskStore.put evicts gates, but gates are added in claimCheck (packages/mcp/src/lib/code-task.ts:441)
  • [minor] Unused default value for the new codeTaskStore parameter (packages/mcp/src/lib/experimental-tools.ts:780)

Full review in the PR comment · Context7 Ops

@context7-ops

context7-ops Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

💬 Not approved yet

For MCP clients that support Tasks, such as Claude Code, the code tools now return a task while a library's source is indexed. The agent gets the real result when the index is ready, for up to 30 minutes. Other clients see the same text as before.

Not approved because a lockfile changed; the bot cannot verify resolved URLs or integrity hashes; it changes security-sensitive code; it is a large change. A person reviews this PR.

Details: findings, what changes, screenshots, API responses

What changes

  • mcp: While a library is being indexed, a code tool call returns an MCP task to clients that declare io.modelcontextprotocol/tasks on a 2026-07-28 request, instead of the "being indexed" text.
  • mcp: tasks/get, tasks/cancel and tasks/update are added. They are bound to the caller and answer -32021 without the Tasks capability and -32602 for an unknown task.
  • mcp: Polls share one Context7 API request per caller and library version, at most every max(retryAfter, 45 s). Timeouts, 5xx and a busy code store keep a task waiting.
  • mcp: An index_pending answer now ends with the API's text and is not marked as an error, both for tasks and for plain calls.
  • internal: The HTTP server stores tasks in Upstash Redis (UPSTASH_REDIS_REST_URL / UPSTASH_REDIS_REST_TOKEN) and creates no tasks without them; stdio keeps tasks in memory.
  • internal: The tasks/get that completes a task records only the result's outcome, so each tool call still sends one authenticated_tool_call event.

Commit e439354

Checks: risk, security, maintainability. Model anthropic/claude-opus-5.5, about $0.07. Ask Context7 Ops in Slack for more pages, a question about the code or a rerun.

…rd task outcomes, and move the task lifecycle to code-task.ts
@context7

context7 Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Docs7 for context7.com

Result Status Action
Deployment ✅ Ready Open preview
Content review ⏳ In progress —

Commit 23d6d4d · Updated 2026-11-27 14:23 UTC · View build details

@context7
context7 Bot temporarily deployed to Docs7 Preview: CTX7-4100/mcp-code-tool-tasks November 27, 2026 11:50 Destroyed

@context7-ops context7-ops Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To fix on commit ba1b9d9 before this PR is ready:

  • [minor] A transient failure after the 30-minute wait releases the version's gate as if the index were done (packages/mcp/src/lib/code-task.ts:319)
  • [minor] A task-backed call that is never polled to completion is never recorded in telemetry (packages/mcp/src/lib/code-tools.ts:430)

Full review in the PR comment · Context7 Ops

Comment thread packages/mcp/src/lib/code-task.ts Resolved
Comment thread packages/mcp/src/lib/code-tools.ts Resolved
@context7-ops
context7-ops Bot dismissed their stale review November 27, 2026 11:51

No blocking findings on commit ba1b9d9.

@context7
context7 Bot temporarily deployed to Docs7 Preview: CTX7-4100/mcp-code-tool-tasks November 27, 2026 11:55 Destroyed

@context7-ops context7-ops Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To fix on commit d3aa514 before this PR is ready:

  • [minor] A tool call that becomes a task is counted twice as an authenticated tool call (packages/mcp/src/lib/code-task.ts:324)

Full review in the PR comment · Context7 Ops

Comment thread packages/mcp/src/lib/code-task.ts Resolved
@context7
context7 Bot temporarily deployed to Docs7 Preview: CTX7-4100/mcp-code-tool-tasks November 27, 2026 11:59 Destroyed

@context7-ops context7-ops Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No findings on commit e439354. A person gives the final approval because a lockfile changed; the bot cannot verify resolved URLs or integrity hashes; it changes security-sensitive code; it is a large change.

Full review in the PR comment · Context7 Ops

@context7-ops
context7-ops Bot requested a review from ramazanoacar November 27, 2026 12:00

@ramazanoacar ramazanoacar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Fahreddin, this is a solid piece of work. Here's what I went through:

  • Task binding and storage: tasks are bound by callerBinding, so only a verified OAuth user stands for a refreshed token, and another caller gets the same "Unknown task" error as an unknown ID. Redis holds the request and a hash of the caller, never the credential.
  • The gate: one gate per caller and library version (normalised the way fetchLibraryCode reads it) keeps parallel greps and globs inside the poll pass. Only a finished index opens it, so a timeout after the wait doesn't release it for the other tasks.
  • Redis scripts: they make the first final status win, so a cancel that lands during the API request is answered from the store.
  • Fallbacks: a client without the extension, a 2025-era request, or a server without a store gets the same text as today. A store failure while creating the task falls back to that text too.
  • Lockfile: it only adds @upstash/redis@1.41.0 and its one dependency, uncrypto@0.1.3.

Approving.

Small notes (non-blocking)

  • UPSTASH_REDIS_REST_URL / UPSTASH_REDIS_REST_TOKEN are the SDK's default names, and the Vercel and Upstash integrations set exactly these. A self-hosted server that already has them for another database would start writing mcp:code-task:* keys there and advertising tasks without anyone choosing it. A prefixed pair (e.g. CONTEXT7_TASKS_REDIS_REST_URL / _TOKEN) would make it opt-in. If you keep the names, a line in the self-hosting docs saying that setting them turns tasks on would help.
  • tasks/* are registered only on a server built with the code tools on. If a user turns the code tools off in the dashboard while a task is running, the next tasks/get gets method-not-found rather than "Unknown task". Clients should handle both the same, so this is just something to know.
  • A completed task answers from the record without the API, so a key revoked within the hour can still read the result of a call it was allowed to make. That seems fine for an hour-long TTL; I'm mentioning it so it's a known choice.

@context7
context7 Bot temporarily deployed to Docs7 Preview: CTX7-4100/mcp-code-tool-tasks November 27, 2026 12:24 Destroyed
@context7
context7 Bot temporarily deployed to Docs7 Preview: CTX7-4100/mcp-code-tool-tasks November 27, 2026 12:43 Destroyed

@ramazanoacar ramazanoacar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Fahreddin, moving the task into the API is a good call. The server is stateless again, my note about the UPSTASH_REDIS_* names no longer applies, and the lockfile change is gone. I read the two new commits against the app-side change (merged):

  • Access: each poll replays the stored request with the poller's own credential, and a stored answer is served again only after the access check. The revoked-key case from my last note is covered now too.
  • Task ownership: the app treats another caller's task as unknown, which matches the -32602 here.
  • Cutoff: the app always sends X-Context7-Task-Created on a poll, so the 30-minute cutoff works from the real creation time.
  • Rate limits: a 429 rate_limited keeps the task waiting. Without the gate, that's what keeps a busy poller from ending its task early.

Approving.

Small notes (non-blocking)

  • cancelCodeTask treats every answer other than 404 and 410 as done, including a 5xx, a 429 or a timeout. A cancel that didn't reach the API returns {}, and the next tasks/get still says working. Throwing an internal error when the DELETE isn't ok (or at least logging it) would let the client know.
  • If a poll ever comes back without X-Context7-Task-Created, createdAt falls back to now on every poll, so the 30-minute cutoff never fires. The app always sends it today. Falling back to CODE_TASK_TTL_MS worth of polls isn't possible without state, so a short comment that the header is required would protect it.

@context7
context7 Bot temporarily deployed to Docs7 Preview: CTX7-4100/mcp-code-tool-tasks November 27, 2026 14:23 Destroyed
@fahreddinozcan
fahreddinozcan merged commit 1783909 into master Nov 27, 2026
4 checks passed
@fahreddinozcan
fahreddinozcan deleted the CTX7-4100/mcp-code-tool-tasks branch November 27, 2026 14:24

This branch was successfully deployed

No deployments
Docs7 Preview: CTX7-4100/mcp-code-tool-tasks — 23d6d4dd Deployed Nov 27, 2026 by context7[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants