-
Notifications
You must be signed in to change notification settings - Fork 3
feat(core): add first-class MCP support #21
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9ce0382
docs: add first-class MCP implementation plan
stanley2058 dd0bc9f
feat(cc): enable native deferred tool search
stanley2058 c4b8820
feat(core): add process-wide MCP registry
stanley2058 bc4bd7b
feat(core): add deferred tool catalog activation
stanley2058 89ef6a9
feat(core): add MCP management and OAuth lifecycle
stanley2058 b274a8b
fix(core): guard MCP credential paths
stanley2058 5422a51
feat(core): seed MCP management guidance
stanley2058 bc59893
docs: mark MCP implementation complete
stanley2058 84454e0
fix(core): address MCP review blockers
stanley2058 d8e94bb
fix(core): harden MCP reload and auth
stanley2058 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
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
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,155 @@ | ||
| import { createHash } from "node:crypto"; | ||
|
|
||
| import { z } from "zod"; | ||
|
|
||
| export const CATALOG_TOOL_ID_VERSION = 1 as const; | ||
| export const MAX_MODEL_TOOL_NAME_LENGTH = 64; | ||
|
|
||
| export const catalogToolIdentitySchema = z.strictObject({ | ||
| source: z.enum(["plugin", "mcp"]), | ||
| sourceId: z.string().min(1), | ||
| rawToolName: z.string().min(1), | ||
| }); | ||
|
|
||
| export type CatalogToolIdentity = z.infer<typeof catalogToolIdentitySchema>; | ||
|
|
||
| const stableIdSchema = z.tuple([ | ||
| z.literal("lilac.catalog-tool"), | ||
| z.literal(CATALOG_TOOL_ID_VERSION), | ||
| z.enum(["plugin", "mcp"]), | ||
| z.string().min(1), | ||
| z.string().min(1), | ||
| ]); | ||
|
|
||
| export type CatalogStableIdParseResult = | ||
| | { ok: true; identity: CatalogToolIdentity } | ||
| | { ok: false; error: string }; | ||
|
|
||
| /** A versioned, delimiter-safe persistence key. */ | ||
| export function catalogToolStableId(identity: CatalogToolIdentity): string { | ||
| const parsed = catalogToolIdentitySchema.parse(identity); | ||
| return JSON.stringify([ | ||
| "lilac.catalog-tool", | ||
| CATALOG_TOOL_ID_VERSION, | ||
| parsed.source, | ||
| parsed.sourceId, | ||
| parsed.rawToolName, | ||
| ]); | ||
| } | ||
|
|
||
| export function parseCatalogToolStableId(stableId: string): CatalogStableIdParseResult { | ||
| let decoded: unknown; | ||
| try { | ||
| decoded = JSON.parse(stableId); | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| return { ok: false, error: `invalid catalog tool ID JSON: ${message}` }; | ||
| } | ||
|
|
||
| const parsed = stableIdSchema.safeParse(decoded); | ||
| if (!parsed.success) return { ok: false, error: z.prettifyError(parsed.error) }; | ||
| return { | ||
| ok: true, | ||
| identity: { source: parsed.data[2], sourceId: parsed.data[3], rawToolName: parsed.data[4] }, | ||
| }; | ||
| } | ||
|
|
||
| function compareText(left: string, right: string): number { | ||
| return left < right ? -1 : left > right ? 1 : 0; | ||
| } | ||
|
|
||
| function normalizeNameSegment(value: string): string { | ||
| const normalized = value.replace(/[^0-9A-Za-z_]+/g, "_").replace(/_+/g, "_"); | ||
| return normalized.replace(/^_+|_+$/g, ""); | ||
| } | ||
|
|
||
| function identityHash(identity: CatalogToolIdentity): string { | ||
| return createHash("sha256").update(catalogToolStableId(identity)).digest("hex").slice(0, 10); | ||
| } | ||
|
|
||
| function appendHash(base: string, identity: CatalogToolIdentity): string { | ||
| const suffix = `_${identityHash(identity)}`; | ||
| const available = MAX_MODEL_TOOL_NAME_LENGTH - suffix.length; | ||
| const stem = base.slice(0, available).replace(/_+$/g, "") || "tool"; | ||
| return `${stem}${suffix}`; | ||
| } | ||
|
|
||
| /** Deterministic candidate before catalog-wide collision resolution. */ | ||
| export function baseCatalogToolName(identity: CatalogToolIdentity): string { | ||
| const parsed = catalogToolIdentitySchema.parse(identity); | ||
| const sourceId = normalizeNameSegment(parsed.sourceId) || "source"; | ||
| const rawToolName = normalizeNameSegment(parsed.rawToolName) || "tool"; | ||
| const base = `${parsed.source}_${sourceId}_${rawToolName}`; | ||
| return base.length <= MAX_MODEL_TOOL_NAME_LENGTH ? base : appendHash(base, parsed); | ||
| } | ||
|
|
||
| export type CatalogToolNameCollision = { | ||
| readonly modelName: string; | ||
| readonly identities: readonly CatalogToolIdentity[]; | ||
| readonly reserved: boolean; | ||
| }; | ||
|
|
||
| export type CatalogToolNameAssignment = { | ||
| /** Stable catalog ID to provider-facing tool name. */ | ||
| readonly byStableId: ReadonlyMap<string, string>; | ||
| /** Provider-facing tool name to structured source identity. */ | ||
| readonly byModelName: ReadonlyMap<string, CatalogToolIdentity>; | ||
| /** Normally empty; detects a reserved or hash-suffix collision without shadowing. */ | ||
| readonly collisions: readonly CatalogToolNameCollision[]; | ||
| }; | ||
|
|
||
| export function assignCatalogToolNames( | ||
| identities: readonly CatalogToolIdentity[], | ||
| reservedNames: ReadonlySet<string> = new Set(), | ||
| ): CatalogToolNameAssignment { | ||
| const unique = new Map<string, CatalogToolIdentity>(); | ||
| for (const identity of identities) { | ||
| const parsed = catalogToolIdentitySchema.parse(identity); | ||
| unique.set(catalogToolStableId(parsed), parsed); | ||
| } | ||
| const sorted = [...unique.entries()].sort(([left], [right]) => compareText(left, right)); | ||
|
|
||
| const baseCounts = new Map<string, number>(); | ||
| for (const [, identity] of sorted) { | ||
| const base = baseCatalogToolName(identity); | ||
| baseCounts.set(base, (baseCounts.get(base) ?? 0) + 1); | ||
| } | ||
|
|
||
| const finalGroups = new Map<string, Array<{ stableId: string; identity: CatalogToolIdentity }>>(); | ||
| for (const [stableId, identity] of sorted) { | ||
| const base = baseCatalogToolName(identity); | ||
| const modelName = | ||
| (baseCounts.get(base) ?? 0) > 1 || reservedNames.has(base) | ||
| ? appendHash(base, identity) | ||
| : base; | ||
| const group = finalGroups.get(modelName) ?? []; | ||
| group.push({ stableId, identity }); | ||
| finalGroups.set(modelName, group); | ||
| } | ||
|
|
||
| const byStableId = new Map<string, string>(); | ||
| const byModelName = new Map<string, CatalogToolIdentity>(); | ||
| const collisions: CatalogToolNameCollision[] = []; | ||
|
|
||
| for (const modelName of [...finalGroups.keys()].sort(compareText)) { | ||
| const group = finalGroups.get(modelName); | ||
| if (!group) continue; | ||
| const reserved = reservedNames.has(modelName); | ||
| if (reserved || group.length > 1) { | ||
| collisions.push({ | ||
| modelName, | ||
| identities: group.map(({ identity }) => identity), | ||
| reserved, | ||
| }); | ||
| continue; | ||
| } | ||
|
|
||
| const onlyEntry = group[0]; | ||
| if (!onlyEntry) continue; | ||
| const { stableId, identity } = onlyEntry; | ||
| byStableId.set(stableId, modelName); | ||
| byModelName.set(modelName, identity); | ||
| } | ||
|
|
||
| return { byStableId, byModelName, collisions }; | ||
| } |
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.