Skip to content

fix(mcp): inline local refs in bundled tool schemas - #81238

Merged
giodl73-repo merged 2 commits into
openclaw:mainfrom
giodl73-repo:fix-mcp-ref-union-schema
May 17, 2026
Merged

giodl73-repo merged 2 commits into
openclaw:mainfrom
giodl73-repo:fix-mcp-ref-union-schema

Conversation

@giodl73-repo

@giodl73-repo giodl73-repo commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #78737.

Bundled MCP tools can expose JSON Schema $defs/definitions plus local $ref pointers. Today those schemas are passed through during MCP tool materialization, so tools like Notion API-post-page can expose a required parent parameter as a local $ref to a oneOf union and fail before the call reaches the MCP server.

This PR:

  • inlines local $ref schemas during tool-parameter normalization;
  • applies the same normalization when materializing bundled MCP tools;
  • preserves the resolved oneOf object union so valid Notion-style parent inputs still validate;
  • adds regression coverage for the provider-neutral normalizer and the bundled MCP materialization boundary.

Validation

  • pnpm test src/agents/pi-tools.schema.test.ts src/agents/pi-bundle-mcp-tools.materialize.test.ts -- --reporter=verbose
  • pnpm exec oxfmt --check --threads=1 src/agents/pi-tools-parameter-schema.ts src/agents/pi-bundle-mcp-materialize.ts src/agents/pi-tools.schema.test.ts src/agents/pi-bundle-mcp-tools.materialize.test.ts CHANGELOG.md
  • pnpm check:changed
  • pnpm check:test-types
  • git diff --check

Real behavior proof

  • Behavior or issue addressed: a bundled MCP tool with a Notion-style parent parameter expressed as $ref: #/$defs/parentRequest, where parentRequest is a oneOf over page/database parent objects, should expose an inlined provider-usable schema and continue to accept a valid { parent: { page_id } } argument before the MCP call.
  • Real environment tested: WSL Ubuntu-24.04 source worktrees created from upstream/main and PR head f4beb91003afbc90b8569e4090399ac6462ff9b8.
  • Exact steps or command run after this patch: created clean detached worktrees for upstream/main and fork/fix-mcp-ref-union-schema; in each worktree ran node --import ./node_modules/tsx/dist/loader.mjs ./probe-mcp-ref-schema.mjs. The probe materialized a real bundled MCP tool named notion__API-post-page from a schema containing $defs, $ref, and oneOf, then ran the real tool-argument validator with { parent: { page_id: "page-id" } }.
  • Evidence after fix (screenshot, recording, terminal capture, console output, redacted runtime log, linked artifact, or copied live output):

PR #81238 bundled MCP ref union schema before/after proof

Fresh proof artifact: https://raw.githubusercontent.com/giodl73-repo/openclaw/proof-artifacts/pr-81238-fresh/pr-81238/pr-81238-bundled-mcp-ref-union-schema-before-after-proof.png
Proof summary: https://raw.githubusercontent.com/giodl73-repo/openclaw/proof-artifacts/pr-81238-fresh/pr-81238/summary.txt

BEFORE upstream/main:
status=0
toolName=notion__API-post-page
schemaHasRef=true
schemaHasDefs=true
parentHasOneOf=false
acceptedPageId=true
validationError=<none>
ok=false

AFTER PR #81238 head:
status=0
toolName=notion__API-post-page
schemaHasRef=false
schemaHasDefs=false
parentHasOneOf=true
acceptedPageId=true
validationError=<none>
ok=true
  • Observed result after fix: upstream/main exposes a bundled MCP tool schema that still contains local $ref/$defs and does not expose parent.oneOf directly. PR head exposes the inlined parent.oneOf schema without $ref/$defs, while the valid Notion page-parent argument still passes validation.
  • What was not tested: no live Notion API call was made. The proof covers OpenClaw's bridge/materialization/validation layer, which is the failing layer described in the issue.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: M maintainer Maintainer-authored PR labels May 13, 2026
@giodl73-repo

Copy link
Copy Markdown
Contributor Author

/review

@clawsweeper

clawsweeper Bot commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@clawsweeper

clawsweeper Bot commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge.

Summary
The PR inlines local JSON Pointer $ref schemas during shared tool-parameter normalization, applies that normalization to bundled MCP tool materialization, adds regression coverage, and updates the changelog.

Reproducibility: no. for the original Notion pre-dispatch failure: maintainer live validation reached Notion on current main and PR head. Yes by source inspection for the PR regression: resolved $ref schemas with validation siblings and recursive local refs are weakened by the proposed resolver.

Real behavior proof
Sufficient (live_output): The PR body and linked summary include before/after live output from real bundled MCP materialization plus the real tool-argument validator, which covers the changed OpenClaw bridge layer.

Next step before merge
A narrow automated repair can preserve $ref siblings and recursive local refs with focused regressions; no product or security decision is needed first.

Security
Cleared: The diff changes TypeScript schema normalization, bundled MCP materialization, tests, and changelog text without dependency, workflow, secret, package-resolution, or code-download changes.

Review findings

  • [P2] Preserve $ref sibling constraints — src/agents/pi-tools-parameter-schema.ts:446-448
  • [P2] Keep cyclic local refs from becoming permissive schemas — src/agents/pi-tools-parameter-schema.ts:430-431
Review details

Best possible solution:

Keep the shared normalizer and bundled MCP materialization boundary, but merge only after the resolver preserves valid $ref sibling constraints and handles recursive local refs without weakening validation.

Do we have a high-confidence way to reproduce the issue?

No for the original Notion pre-dispatch failure: maintainer live validation reached Notion on current main and PR head. Yes by source inspection for the PR regression: resolved $ref schemas with validation siblings and recursive local refs are weakened by the proposed resolver.

Is this the best way to solve the issue?

No. Normalizing local refs at the shared tool-schema boundary is the right shape, but the implementation must preserve JSON Schema 2020-12 sibling constraints and leave recursive refs safe instead of replacing them with permissive schemas.

Full review comments:

  • [P2] Preserve $ref sibling constraints — src/agents/pi-tools-parameter-schema.ts:446-448
    When a local $ref resolves, this branch returns the referenced schema and only copies title, description, and default. JSON Schema 2020-12 allows validation keywords beside $ref, so { $ref: "#/$defs/Id", minLength: 8 } becomes a plain string schema and accepts too-short values; preserve those siblings, for example with allOf, and add a regression.
    Confidence: 0.91
  • [P2] Keep cyclic local refs from becoming permissive schemas — src/agents/pi-tools-parameter-schema.ts:430-431
    A recursive local $ref that is already in refStack returns {} and the parent walk then drops the definition block, so that recursive position validates as anything. Keep the ref plus definitions unresolved, or otherwise represent the cycle without weakening the schema.
    Confidence: 0.82

Overall correctness: patch is incorrect
Overall confidence: 0.9

Acceptance criteria:

  • node scripts/run-vitest.mjs src/agents/pi-tools.schema.test.ts src/agents/pi-bundle-mcp-tools.materialize.test.ts -- --reporter=verbose
  • pnpm exec oxfmt --check --threads=1 src/agents/pi-tools-parameter-schema.ts src/agents/pi-bundle-mcp-materialize.ts src/agents/pi-tools.schema.test.ts src/agents/pi-bundle-mcp-tools.materialize.test.ts CHANGELOG.md
  • git diff --check

What I checked:

  • Current main passes bundled MCP schemas through unchanged: On current main, bundled MCP materialization exposes parameters: tool.inputSchema, so local $ref/$defs schemas from MCP tools are not normalized at this boundary. (src/agents/pi-bundle-mcp-materialize.ts:113, 524185a68ea2)
  • PR applies shared normalization at the bundled MCP boundary: PR head changes bundled MCP materialization to call normalizeToolParameterSchema(tool.inputSchema) before exposing tools. (src/agents/pi-bundle-mcp-materialize.ts:114, 2580fe26b95d)
  • Blocking sibling-keyword loss remains in PR head: When a local $ref resolves, PR head copies the resolved schema and only reapplies title, description, and default, so validation siblings such as minLength, pattern, or object constraints are discarded. (src/agents/pi-tools-parameter-schema.ts:446, 2580fe26b95d)
  • Recursive local refs become permissive schemas: A repeated local $ref in the resolver stack returns {} without preserving the unresolved ref or definition block, which weakens recursive schema validation instead of keeping the original contract. (src/agents/pi-tools-parameter-schema.ts:430, 2580fe26b95d)
  • PR tests cover Notion-style refs but not the blocker: The added tests cover inlining a $defs oneOf, nested JSON Pointer refs, and unresolved local refs, but not $ref siblings or recursive local refs. (src/agents/pi-tools.schema.test.ts:139, 2580fe26b95d)
  • MCP SDK accepts broader JSON Schema keywords: The pinned MCP SDK exposes tool inputSchema as an object with type, optional properties/required, and a catchall for additional JSON Schema keywords. (package.json:1766, 524185a68ea2)

Likely related people:

  • steipete: Recent commits touch shared tool schema normalization and bundled MCP materialization/tests, including the central files this PR changes. (role: recent schema-runtime and bundled MCP contributor; confidence: high; commits: 862be9fb3d6c, 54d82b3055af, 3ea27c63e2fc; files: src/agents/pi-tools-parameter-schema.ts, src/agents/pi-bundle-mcp-materialize.ts, src/agents/pi-bundle-mcp-tools.materialize.test.ts)
  • Frank Yang: Earlier merged work changed bundled MCP runtime behavior adjacent to the materialization path reviewed here. (role: adjacent bundled MCP runtime contributor; confidence: medium; commits: 43cd29c4afee; files: src/agents/pi-bundle-mcp-materialize.ts, src/agents/pi-bundle-mcp-runtime.ts)

Remaining risk / open question:

  • The original linked Notion pre-dispatch rejection remains unconfirmed because maintainer live validation reached Notion on both current main and PR head.

Codex review notes: model gpt-5.5, reasoning high; reviewed against 524185a68ea2.

@giodl73-repo
giodl73-repo force-pushed the fix-mcp-ref-union-schema branch from 59894ff to 3c943e6 Compare May 13, 2026 01:18
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 13, 2026
@giodl73-repo
giodl73-repo force-pushed the fix-mcp-ref-union-schema branch from 3c943e6 to c1be32f Compare May 13, 2026 04:53
@giodl73-repo

Copy link
Copy Markdown
Contributor Author

Rebased on current origin/main and reran focused verification.\n\nChecks:\n- oxfmt --check changed files\n- node scripts/test-projects.mjs src/agents/pi-bundle-mcp-tools.materialize.test.ts src/agents/pi-tools.schema.test.ts --reporter=verbose\n- node scripts/check-changed.mjs\n- git diff --check\n\n@clawsweeper re-review

@galiniliev galiniliev 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.

PR #81238 fixes the right bundled MCP boundary for issue #78737 by normalizing tool.inputSchema before exposing MCP tools. The direct Notion-style $ref: "#/$defs/parentRequest" case is covered, but the resolver currently breaks valid JSON Schema refs outside that narrow shape.

By: Gio Della-Libera (@giodl73-repo, acct 2025-09-30) | OpenClaw: 17 PRs, 1 issue, 13 commits/12mo | GitHub: 21 repos, 3353 commits, 40 PRs, 8 issues, 2 reviews
Issue opener: @cozywithher (acct 2025-12-03) | OpenClaw: 0 PRs, 2 issues, 0 commits/12mo | GitHub: 0 commits, 0 PRs, 2 issues, 0 reviews

Findings

  • Blocking: inlineLocalSchemaRefsWithDefs only resolves refs matching #/$defs/<name> or #/definitions/<name>, but it removes every $defs / definitions object while walking the schema. A valid schema such as properties.x.$ref = "#/$defs/Foo/properties/bar" is not resolved by the regex at src/agents/pi-tools-parameter-schema.ts:198, then the root $defs is dropped at src/agents/pi-tools-parameter-schema.ts:243, leaving a dangling ref that local validation rejects. MCP's SDK contract exposes inputSchema as a JSON Schema object (node_modules/@modelcontextprotocol/sdk/dist/esm/spec.types.d.ts:1180), and TypeBox currently validates that nested JSON Pointer form correctly when $defs is retained. Best fix: either implement full local JSON Pointer resolution or preserve $defs/definitions whenever unresolved local refs remain. Add a regression with a nested pointer ref.

Verdict
Request changes. The approach is right for the reported Notion direct-ref bug, and src/agents/pi-bundle-mcp-materialize.ts:114 is the right boundary, but the shared normalizer must not make broader valid MCP schemas invalid.

Evidence read: linked issue #78737, PR diff, current materialization path, pi-ai validateToolArguments using TypeBox compile, MCP SDK tool schema types, and the PR tests. CI on the PR is green, but GitHub currently reports the PR as conflicting due to CHANGELOG.md, which is landing cleanup rather than the review finding.

@giodl73-repo
giodl73-repo force-pushed the fix-mcp-ref-union-schema branch 4 times, most recently from b80c2ab to 9a54635 Compare May 13, 2026 10:21
@giodl73-repo

Copy link
Copy Markdown
Contributor Author

Fixed the nested local JSON Pointer review finding and rebased on current origin/main.

Changes:

  • resolve #/$defs/Foo/properties/bar and #/definitions/Foo/properties/bar style refs
  • keep $defs and definitions namespaces separate
  • preserve local definitions when an unresolved local ref remains so schemas do not become dangling
  • added regressions for nested pointer refs and unresolved local refs

Focused verification:

  • node scripts/test-projects.mjs src/agents/pi-tools.schema.test.ts src/agents/pi-bundle-mcp-tools.materialize.test.ts --reporter=verbose
  • git diff --check

@clawsweeper re-review

@giodl73-repo
giodl73-repo force-pushed the fix-mcp-ref-union-schema branch from 9a54635 to 0edf3de Compare May 13, 2026 13:34
@giodl73-repo

Copy link
Copy Markdown
Contributor Author

Rebased again onto current origin/main after the latest main movement. The nested JSON Pointer fix is still unchanged on the new head.

Focused verification after this rebase:

  • node scripts/test-projects.mjs src/agents/pi-tools.schema.test.ts src/agents/pi-bundle-mcp-tools.materialize.test.ts --reporter=verbose
  • git diff --check

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@altaywtf altaywtf self-assigned this May 14, 2026
@altaywtf

Copy link
Copy Markdown
Member

Maintainer live validation update: I ran a real OpenClaw live probe on Crabbox/Blacksmith, not just the focused fixture tests.

Setup:

  • Testbox tbx_01krjzmsbter9fn5s4xrk2t4gd, Actions run 25854517169
  • openclaw agent --local with live OpenAI gpt-5.4
  • real configured Notion MCP server via npx -y @notionhq/[email protected]
  • only exposed tool: notion__API-post-page
  • tool args used a valid UUID-shaped parent.page_id: 11111111-1111-1111-1111-111111111111

Result: both PR head and current origin/main successfully called the Notion MCP tool and reached Notion. Both returned the downstream Notion response 401 unauthorized / API token is invalid, which is expected without a real Notion token.

So I could not reproduce the issue as “OpenClaw rejects the valid Notion parent shape before the call reaches the MCP server” on current main. The request crossed the OpenClaw validation/materialization boundary on main already.

The PR still appears useful as schema-normalization / provider-compat cleanup: PR head exposes the smaller inlined schema, while main exposes the raw $defs/$ref shape and logs a strict-schema downgrade for OpenAI Responses. I would frame this as improving bundled MCP tool schema compatibility unless there is another repro path that still shows current main rejecting the parent argument before dispatch.

@clawsweeper

clawsweeper Bot commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@giodl73-repo
giodl73-repo force-pushed the fix-mcp-ref-union-schema branch from fbc239c to fec810f Compare May 15, 2026 02:30
@giodl73-repo

Copy link
Copy Markdown
Contributor Author

Addressed the review feedback: the schema normalizer now resolves nested local JSON Pointer refs under both $defs and definitions, and preserves definition blocks when unresolved local refs remain so valid MCP schemas do not become dangling-ref schemas. Added regressions for nested pointer refs and unresolved local refs, then rebased onto current upstream/main.

@giodl73-repo
giodl73-repo force-pushed the fix-mcp-ref-union-schema branch 3 times, most recently from edc559d to 2fdad23 Compare May 16, 2026 06:20
@giodl73-repo
giodl73-repo force-pushed the fix-mcp-ref-union-schema branch 3 times, most recently from 78130ab to a02cd9d Compare May 16, 2026 14:11
@giodl73-repo
giodl73-repo force-pushed the fix-mcp-ref-union-schema branch from a02cd9d to f4beb91 Compare May 16, 2026 14:16
@giodl73-repo

Copy link
Copy Markdown
Contributor Author

Updated #81238 with a fresh deterministic proof image and gate-friendly proof section.

The proof compares upstream/main against PR head f4beb91003afbc90b8569e4090399ac6462ff9b8 using the real bundled MCP materialization path and real tool-argument validator:

  • Before: exposed schema still has $ref/$defs and no direct parent.oneOf.
  • After: exposed schema has no $ref/$defs, has direct parent.oneOf, and still accepts { parent: { page_id: "page-id" } }.

Fresh proof image: https://raw.githubusercontent.com/giodl73-repo/openclaw/proof-artifacts/pr-81238-fresh/pr-81238/pr-81238-bundled-mcp-ref-union-schema-before-after-proof.png
Proof summary: https://raw.githubusercontent.com/giodl73-repo/openclaw/proof-artifacts/pr-81238-fresh/pr-81238/summary.txt

@clawsweeper clawsweeper Bot added the P2 Normal backlog priority with limited blast radius. label May 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling maintainer Maintainer-authored PR P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP tool bridge: $ref/oneOf schemas not resolved for Notion API-post-page parent parameter

3 participants