-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Harden plugin MCP tool registration #988
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
masonjames
wants to merge
14
commits into
emdash-cms:main
Choose a base branch
from
masonjames:codex/plugin-mcp-tools
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c61e4e3
Add plugin-defined MCP tools
masonjames 29aa5ba
Address plugin MCP tool review feedback
masonjames 557aa1e
Fix manifest schema projection validation
masonjames a78ec24
Merge branch 'main' into codex/plugin-mcp-tools
ascorbic ad3bce2
Address MCP JSON schema review follow-up
masonjames c66be2d
Merge branch 'main' into codex/plugin-mcp-tools
masonjames b0200a4
Merge branch 'main' into codex/plugin-mcp-tools
ascorbic 84dbc42
Merge upstream main into plugin MCP tools
masonjames 6055f5e
Merge branch 'main' into codex/plugin-mcp-tools
masonjames 334c1ac
Merge remote-tracking branch 'origin/main' into codex/plugin-mcp-tools
masonjames 60f1316
fix changeset package name after main sync
masonjames d6ff368
Merge remote-tracking branch 'origin/main' into codex/plugin-mcp-tools
masonjames cd70c91
Merge branch 'main' into codex/plugin-mcp-tools
masonjames e7f2a51
Merge remote-tracking branch 'origin/main' into codex/plugin-mcp-tools
masonjames File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "emdash": patch | ||
| --- | ||
|
|
||
| Fixes plugin MCP registration for scoped IDs and malformed serialized schemas so valid tools remain available instead of emitting invalid names or disabling the MCP endpoint. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| const fallbackSchema = z.record(z.string(), z.unknown()); | ||
|
|
||
| export function safeJsonSchemaToZod( | ||
| schema: Record<string, unknown>, | ||
| onError?: (error: unknown) => void, | ||
| ): z.ZodType { | ||
| try { | ||
| return z.fromJSONSchema(schema); | ||
| } catch (error) { | ||
| onError?.(error); | ||
| return fallbackSchema; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| const FORWARD_SLASH_PATTERN = "/"; | ||
| const MCP_SAFE_PLUGIN_ID_PATTERN = /^[A-Za-z0-9_.-]+$/; | ||
| const PLUGIN_SCOPE_PREFIX_PATTERN = /^@/; | ||
| const PLUGIN_ID_DASH_PATTERN = /-/g; | ||
| const PLUGIN_ID_UNDERSCORE_PATTERN = /_/; | ||
| const AMBIGUOUS_PLUGIN_SEGMENT_PATTERN = /__/; | ||
| const AMBIGUOUS_JOINED_PLUGIN_ID_PATTERN = /_{3,}/; | ||
|
|
||
| export function pluginToolName(pluginId: string, toolName: string): string { | ||
| if ( | ||
| MCP_SAFE_PLUGIN_ID_PATTERN.test(pluginId) && | ||
| !AMBIGUOUS_PLUGIN_SEGMENT_PATTERN.test(pluginId) | ||
| ) { | ||
| return `${pluginId}__${toolName}`; | ||
| } | ||
|
|
||
| const readableSegments = pluginId | ||
| .replace(PLUGIN_SCOPE_PREFIX_PATTERN, "") | ||
| .split(FORWARD_SLASH_PATTERN) | ||
| .map((segment) => segment.replace(PLUGIN_ID_DASH_PATTERN, "_")); | ||
| const readablePluginId = readableSegments.join("__"); | ||
| if ( | ||
| PLUGIN_ID_UNDERSCORE_PATTERN.test(pluginId) || | ||
| AMBIGUOUS_JOINED_PLUGIN_ID_PATTERN.test(readablePluginId) || | ||
| readableSegments.some((segment) => AMBIGUOUS_PLUGIN_SEGMENT_PATTERN.test(segment)) | ||
| ) { | ||
| return `${readablePluginId}__${stableToolNameHash(pluginId)}__${toolName}`; | ||
| } | ||
| return `${readablePluginId}__${toolName}`; | ||
| } | ||
|
|
||
| function stableToolNameHash(value: string): string { | ||
| let hash = 0x811c9dc5; | ||
| for (let index = 0; index < value.length; index++) { | ||
| hash ^= value.charCodeAt(index); | ||
| hash = Math.imul(hash, 0x01000193); | ||
| } | ||
| return (hash >>> 0).toString(16).padStart(8, "0"); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { safeJsonSchemaToZod } from "../../../src/mcp/json-schema.js"; | ||
|
|
||
| describe("safeJsonSchemaToZod", () => { | ||
| it("falls back instead of throwing for a malformed serialized schema", () => { | ||
| const onError = vi.fn(); | ||
| const fallback = safeJsonSchemaToZod({ type: "definitely-not-json-schema" }, onError); | ||
|
|
||
| expect(() => fallback.parse({ unexpected: true })).not.toThrow(); | ||
| expect(onError).toHaveBeenCalledOnce(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { pluginToolName } from "../../../src/mcp/plugin-tool-name.js"; | ||
|
|
||
| const HASHED_TOOL_NAME_PATTERN = /^[a-z0-9_]+__[0-9a-f]{8}__[a-z][a-z0-9_]*$/; | ||
|
|
||
| describe("pluginToolName", () => { | ||
| it("uses readable names for unambiguous scoped plugin IDs", () => { | ||
| expect(pluginToolName("@emdash-cms/plugin-forms", "submit_form")).toBe( | ||
| "emdash_cms__plugin_forms__submit_form", | ||
| ); | ||
| }); | ||
|
|
||
| it("preserves existing names for already MCP-safe plugin IDs", () => { | ||
| expect(pluginToolName("calendar-plugin", "createEvent")).toBe("calendar-plugin__createEvent"); | ||
| expect(pluginToolName("foo--bar", "summarize")).toBe("foo--bar__summarize"); | ||
| }); | ||
|
|
||
| it("hashes safe-looking IDs with ambiguous separators", () => { | ||
| expect(pluginToolName("foo__bar", "summarize")).toMatch(HASHED_TOOL_NAME_PATTERN); | ||
| }); | ||
|
|
||
| it("keeps scoped IDs distinct when dashes sit next to the slash boundary", () => { | ||
| const trailingDashScope = pluginToolName("@a-/b", "summarize"); | ||
| const leadingDashName = pluginToolName("@a/-b", "summarize"); | ||
|
|
||
| expect(trailingDashScope).toMatch(HASHED_TOOL_NAME_PATTERN); | ||
| expect(leadingDashName).toMatch(HASHED_TOOL_NAME_PATTERN); | ||
| expect(trailingDashScope).not.toBe(leadingDashName); | ||
| }); | ||
|
|
||
| it("keeps normalized dashes distinct from literal underscores", () => { | ||
| expect(pluginToolName("foo-bar", "summarize")).not.toBe(pluginToolName("foo_bar", "summarize")); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.