Skip to content

Add token-budgeted packs and a stdio MCP server - #7

Merged
cursor[bot] merged 1 commit into
mainfrom
cursor/mcp-pack-context-cd4a
Oct 22, 2026
Merged

cursor[bot] merged 1 commit into
mainfrom
cursor/mcp-pack-context-cd4a

Conversation

@yniantongtian-oss

Copy link
Copy Markdown
Owner

What

  • contextgraph pack --budget N exports ranked source that fits an approximate token budget. Each item records rank, score, path, and line span; the source itself appears once in context.
  • Optional extra contextgraph-ai[mcp] adds contextgraph mcp, a stdio server on the official MCP Python SDK. Tools: build_graph, query_context, and pack_context.
  • README documents Claude Code (.mcp.json) and Cursor (.cursor/mcp.json) config.

Why

Coding agents need a small, local, ranked slice of a repository instead of a whole file pasted twice. The same ranker now backs the CLI and the MCP tools, so an editor and a pipe see one behavior. The server does not execute indexed code and does not open a port.

Bug fixes found while wiring this

  • Call edges no longer attach every same-named symbol. Bare calls prefer a nested or module-level function; self.method() stays on the enclosing class. Ambiguous names are left unresolved.
  • Packing suppresses a file, class, or method whose span contains or is contained by a block already chosen, and a short later hit can still use leftover budget under 80 characters.
  • scan includes parse and load messages. pack and bundle send index progress to stderr and print unwrapped JSON, so stdout can be piped.

How verified

  • ruff check and ruff format --check on src, tests, and examples
  • pytest --cov=contextgraph (86% coverage, gate is 80%)
  • python -m build and python -m twine check dist/*
  • Installed-wheel smoke: public imports plus contextgraph --help and contextgraph pack graph --project <repo> --budget 200
  • python examples/backend_demo.py
Open in Web Open in Cursor 

Agents can export ranked source with pack --budget, and Claude Code or Cursor can call the same build, query, and pack operations over stdio. Call resolution and packing no longer duplicate or mis-attach symbols.

Co-authored-by: Gan shihao <yniantongtian-oss@users.noreply.github.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-22T15:34:05.905492Z 8940d2b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@cursor
cursor Bot merged commit 9fd9f68 into main Oct 22, 2026
11 checks passed
@cursor
cursor Bot deleted the cursor/mcp-pack-context-cd4a branch October 22, 2026 15:32

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8940d2bf8b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/contextgraph/core.py
Comment on lines +105 to +107
module_level = [node_id for node_id in local if "." not in qualname(node_id)]
if len(module_level) == 1:
return module_level

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Resolve enclosing function scopes before module-level names

When a nested function calls a sibling defined in an enclosing function, this fallback can select a different module-level function instead. For example, with both module-level helper and outer.helper, a call to helper() inside outer.run now resolves exclusively to the module-level symbol, removing the actual call edge that the previous implementation included. Walk enclosing function scopes before falling back to module-level names so closure calls retain their correct targets and graph-derived ranking.

Useful? React with 👍 / 👎.

Comment thread src/contextgraph/core.py
Comment on lines +624 to +628
later = scored_nodes[index + 1 :]
if span is not None and any(
self._strictly_contains(span, inner)
for other_id, _other_score in later
if (inner := self._node_span(other_id)) is not None

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid scanning all later candidates when too little budget remains

When the remaining budget cannot accommodate any oversized block, each candidate still scans all later candidates for contained spans before reaching the minimum-budget check. With disjoint function spans this produces quadratic work, including after an ordinary pack leaves a small unusable remainder. Exercising this packing loop with 8,000 disjoint candidates and a 10-token budget performed 32,004,000 span lookups and took about 24 seconds, despite returning no context. Check the minimum truncation budget before this search and avoid repeated full-tail scans when determining whether a container has a later descendant.

Useful? React with 👍 / 👎.

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