diff --git a/.cursor-plugin/plugin.json b/.cursor-plugin/plugin.json index c2d4111..ef7b83e 100644 --- a/.cursor-plugin/plugin.json +++ b/.cursor-plugin/plugin.json @@ -1,7 +1,7 @@ { "name": "parseable", "displayName": "Parseable", - "version": "0.3.3", + "version": "0.3.4", "description": "Query and manage Parseable logs, metrics, datasets, and alerts from Cursor.", "author": { "name": "Parseable" diff --git a/package-lock.json b/package-lock.json index df45ef4..9e55999 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@parseable/parseable-mcp-server", - "version": "0.3.3", + "version": "0.3.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@parseable/parseable-mcp-server", - "version": "0.3.3", + "version": "0.3.4", "license": "Apache-2.0", "dependencies": { "@hono/node-server": "^2.0.12", diff --git a/package.json b/package.json index 4b0d83b..65cd404 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@parseable/parseable-mcp-server", - "version": "0.3.3", + "version": "0.3.4", "mcpName": "io.github.parseablehq/parseable-mcp-server", "description": "Model Context Protocol server for Parseable. Lets LLMs discover and query Parseable datasets.", "repository": { diff --git a/server.json b/server.json index 153a2a0..5acb588 100644 --- a/server.json +++ b/server.json @@ -7,12 +7,12 @@ "url": "https://github.com/parseablehq/parseable-mcp-server", "source": "github" }, - "version": "0.3.3", + "version": "0.3.4", "packages": [ { "registryType": "npm", "identifier": "@parseable/parseable-mcp-server", - "version": "0.3.3", + "version": "0.3.4", "transport": { "type": "stdio" } diff --git a/src/bootstrap.ts b/src/bootstrap.ts index e7e13a7..583611c 100644 --- a/src/bootstrap.ts +++ b/src/bootstrap.ts @@ -10,7 +10,7 @@ import { PARSEABLE_ICON_DATA_URI } from "./icon.js"; import { getRemoteTools } from "./remote-tools.js"; import { filterToolsForProfile, type ToolProfile, toolAllowed } from "./tool-profile.js"; -const SERVER_VERSION = "0.3.3"; +const SERVER_VERSION = "0.3.4"; function toolError(message: string): CallToolResult { return { isError: true, content: [{ type: "text", text: message }] }; diff --git a/src/remote-tools.ts b/src/remote-tools.ts index a19f4e5..2bd2232 100644 --- a/src/remote-tools.ts +++ b/src/remote-tools.ts @@ -54,6 +54,107 @@ function cacheKey(client: ParseableClient): string { return createHash("sha256").update(identity).digest("hex"); } +function isSchema(value: unknown): value is Record { + return typeof value === "object" && value !== null && !Array.isArray(value); +} + +function schemaList(value: unknown): Record[] { + return Array.isArray(value) ? value.filter(isSchema) : []; +} + +function requiredFields(schema: Record): string[] { + return Array.isArray(schema.required) + ? schema.required.filter((field): field is string => typeof field === "string") + : []; +} + +function mergeProperty( + existing: unknown, + incoming: unknown, + combinator: "anyOf" | "allOf", +): unknown { + if (existing === undefined || JSON.stringify(existing) === JSON.stringify(incoming)) { + return incoming; + } + const alternatives = + isSchema(existing) && Object.keys(existing).length === 1 && Array.isArray(existing[combinator]) + ? [...(existing[combinator] as unknown[])] + : [existing]; + if (!alternatives.some((value) => JSON.stringify(value) === JSON.stringify(incoming))) { + alternatives.push(incoming); + } + return { [combinator]: alternatives }; +} + +/** + * Keep advertised MCP tools compatible with function-calling providers that + * require an object root and reject top-level JSON Schema combinators. + */ +export function normalizeInputSchema(input: RemoteTool["inputSchema"]): RemoteTool["inputSchema"] { + const source = input as Record; + const unionBranches = [...schemaList(source.oneOf), ...schemaList(source.anyOf)].map((branch) => + normalizeInputSchema(branch as RemoteTool["inputSchema"]), + ); + const intersectionBranches = schemaList(source.allOf).map((branch) => + normalizeInputSchema(branch as RemoteTool["inputSchema"]), + ); + if (unionBranches.length === 0 && intersectionBranches.length === 0) return input; + + const properties: Record = isSchema(source.properties) + ? { ...source.properties } + : {}; + + const unionProperties: Record = {}; + for (const branch of unionBranches) { + const branchProperties = isSchema(branch.properties) ? branch.properties : {}; + for (const [name, propertySchema] of Object.entries(branchProperties)) { + unionProperties[name] = mergeProperty(unionProperties[name], propertySchema, "anyOf"); + } + } + for (const [name, propertySchema] of Object.entries(unionProperties)) { + properties[name] = mergeProperty(properties[name], propertySchema, "allOf"); + } + + for (const branch of intersectionBranches) { + const branchProperties = isSchema(branch.properties) ? branch.properties : {}; + for (const [name, propertySchema] of Object.entries(branchProperties)) { + properties[name] = mergeProperty(properties[name], propertySchema, "allOf"); + } + } + + const required = new Set(requiredFields(source)); + for (const branch of intersectionBranches) { + for (const field of requiredFields(branch)) required.add(field); + } + if (unionBranches.length > 0) { + const common = new Set(requiredFields(unionBranches[0])); + for (const branch of unionBranches.slice(1)) { + const branchRequired = new Set(requiredFields(branch)); + for (const field of common) { + if (!branchRequired.has(field)) common.delete(field); + } + } + for (const field of common) required.add(field); + } + + const { + oneOf: _oneOf, + anyOf: _anyOf, + allOf: _allOf, + enum: _enum, + const: _const, + not: _not, + required: _required, + ...base + } = source; + return { + ...base, + type: "object", + properties, + ...(required.size > 0 ? { required: [...required] } : {}), + } as RemoteTool["inputSchema"]; +} + function validateTool(value: unknown): RemoteTool { if (!value || typeof value !== "object" || Array.isArray(value)) { throw new Error("Parseable tool registry contains a non-object tool."); @@ -70,7 +171,8 @@ function validateTool(value: unknown): RemoteTool { ) { throw new Error(`Parseable tool ${tool.name} has an invalid inputSchema.`); } - return tool as unknown as RemoteTool; + const validated = tool as unknown as RemoteTool; + return { ...validated, inputSchema: normalizeInputSchema(validated.inputSchema) }; } function validateRegistry(value: unknown): RemoteTool[] { diff --git a/test/remote_tools.test.ts b/test/remote_tools.test.ts index c0d12c2..d9a8021 100644 --- a/test/remote_tools.test.ts +++ b/test/remote_tools.test.ts @@ -1,6 +1,11 @@ import { afterEach, describe, expect, it, vi } from "vitest"; import type { ParseableClient } from "../src/client.js"; -import { clearToolRegistryCache, getRemoteTools, parseCacheMaxAge } from "../src/remote-tools.js"; +import { + clearToolRegistryCache, + getRemoteTools, + normalizeInputSchema, + parseCacheMaxAge, +} from "../src/remote-tools.js"; function mockClient(fetchToolRegistry: ReturnType, apiKey = "key") { return { @@ -28,6 +33,152 @@ afterEach(() => { }); describe("remote tool registry", () => { + it("normalizes top-level unions for function-calling providers", () => { + expect( + normalizeInputSchema({ + type: "object", + anyOf: [ + { + type: "object", + properties: { dataset: { type: "string" }, query: { type: "string" } }, + required: ["dataset", "query"], + }, + { + type: "object", + properties: { dataset: { type: "string" }, trace_id: { type: "string" } }, + required: ["dataset", "trace_id"], + }, + ], + }), + ).toEqual({ + type: "object", + properties: { + dataset: { type: "string" }, + query: { type: "string" }, + trace_id: { type: "string" }, + }, + required: ["dataset"], + }); + }); + + it("keeps conflicting union field types below the object root", () => { + expect( + normalizeInputSchema({ + type: "object", + oneOf: [ + { type: "object", properties: { limit: { type: "integer" } } }, + { type: "object", properties: { limit: { type: "string" } } }, + ], + }), + ).toEqual({ + type: "object", + properties: { + limit: { anyOf: [{ type: "integer" }, { type: "string" }] }, + }, + }); + }); + + it("preserves fields and requirements from unions nested inside intersections", () => { + expect( + normalizeInputSchema({ + type: "object", + allOf: [ + { + anyOf: [ + { + type: "object", + properties: { + dataset: { type: "string" }, + query: { type: "string" }, + }, + required: ["dataset", "query"], + }, + { + type: "object", + properties: { + dataset: { type: "string" }, + trace_id: { type: "string" }, + }, + required: ["dataset", "trace_id"], + }, + ], + }, + ], + }), + ).toEqual({ + type: "object", + properties: { + dataset: { type: "string" }, + query: { type: "string" }, + trace_id: { type: "string" }, + }, + required: ["dataset"], + }); + }); + + it("intersects constraints for the same property across allOf branches", () => { + expect( + normalizeInputSchema({ + type: "object", + allOf: [ + { + type: "object", + properties: { limit: { type: "integer", minimum: 0 } }, + }, + { + type: "object", + properties: { limit: { type: "integer", maximum: 100 } }, + }, + ], + }), + ).toEqual({ + type: "object", + properties: { + limit: { + allOf: [ + { type: "integer", minimum: 0 }, + { type: "integer", maximum: 100 }, + ], + }, + }, + }); + }); + + it("keeps sibling constraints when extending a property that already has allOf", () => { + expect( + normalizeInputSchema({ + type: "object", + properties: { + limit: { + type: "integer", + minimum: 0, + allOf: [{ maximum: 100 }], + }, + }, + allOf: [ + { + type: "object", + properties: { limit: { multipleOf: 2 } }, + }, + ], + }), + ).toEqual({ + type: "object", + properties: { + limit: { + allOf: [ + { + type: "integer", + minimum: 0, + allOf: [{ maximum: 100 }], + }, + { multipleOf: 2 }, + ], + }, + }, + }); + }); + it("validates and caches registry per credential scope", async () => { const fetchRegistry = vi.fn().mockResolvedValue({ notModified: false,