Skip to content

fix(pi): emit parameters and execute in the generated pi adapter - #1678

Open
musichen wants to merge 3 commits into
DeusData:mainfrom
musichen:fix/pi-extension-tool-schemas
Open

fix(pi): emit parameters and execute in the generated pi adapter#1678
musichen wants to merge 3 commits into
DeusData:mainfrom
musichen:fix/pi-extension-tool-schemas

Conversation

@musichen

@musichen musichen commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Problem

The generated Pi extension (~/.pi/agent/extensions/cbmem.ts) registers each MCP tool as:

pi.registerTool({ name: 'index_repository', run: (args, ctx) => call('index_repository', args, ctx?.signal) });

Pi's ToolDefinition requires label, description, parameters, and execute. The run-only shape is accepted by Pi's loader but produces tools that fail in two ways:

  1. No parameters schema - strict providers such as xAI/Grok reject the request with 422 missing field parameters (OpenAI and other providers tolerate the omission, so it only surfaces on some providers).
  2. No execute - the tool is uncallable, because Pi invokes execute, never run.

Once execute was wired up, a third problem surfaced: execute forwarded the raw MCP JSON (which has no content array) instead of Pi's required result shape, crashing the TUI's getTextOutput on result.content.filter(...).

Fix

The adapter now emits the full tool shape from the registry:

pi.registerTool({
  name: 'index_repository',
  label: 'Index repository',
  description: '...',
  parameters: { ...input_schema... },
  execute: async (args, ctx) => {
    const result = await call('index_repository', args, ctx?.signal);
    if (result && typeof result === 'object' && result.error) {
      throw new Error(String(result.error));
    }
    const content = result && Array.isArray(result.content)
      ? result.content
      : [{ type: 'text', text: JSON.stringify(result ?? null, null, 2) }];
    return { content, details: result ?? {} };
  },
});

Specifically:

  • Added cbm_mcp_tool_title and cbm_mcp_tool_description accessors so the adapter reads metadata from the same registry that backs tools/list, instead of drifting.
  • The input_schema is embedded directly as a JSON object literal (compact JSON is valid JavaScript), avoiding a JSON.parse indirection.
  • Added a JS-string escaping helper so long descriptions with apostrophes/backslashes/newlines serialize safely.
  • call now passes --json so the CLI emits the raw MCP result instead of human-readable text that JSON.parse cannot parse.
  • execute returns Pi's required { content, details } shape: it passes the MCP content array through, throws on transport errors, and stringifies anything else.

Tests

  • Added client_adapter_pi_emits_parameters_and_execute asserting execute/parameters are present, the legacy run: shape is gone, the schema is embedded, and --json is requested.
  • Built the CLI and verified the generated cbmem.ts loads and returns a valid result shape for success, error, null, and plain-object results.
  • agent_clients suite: 32/32 passing.

The generated Pi extension registered each MCP tool as { name, run }, but
Pi's ToolDefinition requires label, description, parameters, and execute.
Tools registered that way carried no parameter schema, so strict providers
such as xAI/Grok reject the request with a 422 'missing field parameters',
and the tools were uncallable because Pi invokes execute, never run.

Emit the full tool shape from the registry: label/description via new
accessors, the input_schema embedded directly as a JSON object literal, and
execute instead of run.

Signed-off-by: Alex Musichen <alex.musichen@gmail.com>
@musichen
musichen requested a review from DeusData as a code owner August 16, 2026 14:42
@github-actions

Copy link
Copy Markdown

Thanks for opening this — it has been seen, and it is queued.

This note is automated, but it is not a brush-off: it exists so you know where your PR stands instead of having to guess from silence.

Current review status: working through a backlog. 0.9.1-rc.1 is out, so the release freeze that held reviews is over — but it left a large queue of open pull requests behind it, and we are reading through them oldest-first. The background is in discussion #1144.

What that means for this PR, concretely:

  • It will not be closed for inactivity. No stale bot touches pull requests here.
  • It may still sit a while before a human reads it. That is on us, not on you.
  • Older PRs are read first, so a recent one is not being skipped — it is behind a queue.

Things that will genuinely speed it up whenever review does happen:

  • Keep it rebased on main — the tree is moving quickly right now, and a conflicting branch cannot be reviewed as the diff you intended.
  • Get CI green, or say which failures you believe are pre-existing.
  • Keep the change to one claim. Bundled features and refactors get split before they get merged, which costs you a round trip.
  • Every commit needs a sign-off (git commit -s) — CI enforces DCO.

If this fixes a bug, a reproduction we can run is worth more than a description of the symptom.

Thanks for contributing, and sorry in advance for the wait.

The generated execute forwarded the raw MCP JSON directly, but pi's
ToolDefinition.execute must return { content: [{ type: 'text', text }],
details } — a result without a content array crashes the TUI's
getTextOutput on result.content.filter(...).

Request raw JSON from the CLI ('--json') so the bridge parses the MCP
result instead of the human-readable text, then wrap it: pass the content
array through, throw on transport errors, and stringify anything else.
Adds coverage asserting the corrected execute shape and the --json flag.

Signed-off-by: Alex Musichen <alex.musichen@gmail.com>
clang-format wants no spaces inside a braced initializer.

Signed-off-by: Alex Musichen <alex.musichen@gmail.com>
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.

1 participant