Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions apps/sim/lib/api/contracts/knowledge/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,30 @@ export const organizationKnowledgeMcpContract = defineRouteContract({

const LIVE_SEARCH_MCP_DEFAULT_TOP_K = { federated: 30, scoped: 20 } as const

const HAS_UTC_OFFSET = /(Z|[+-]\d{2}:\d{2})$/

/**
* A datetime field without the regex its JSON Schema would otherwise carry beside
* `format: "date-time"`. Every tools/list call resends the schema, and parsing still rejects a
* string that is not an ISO datetime with an offset.
* An ISO datetime filter that reads a value without an offset as UTC, which is what a client
* resends after that value is rejected, so the call succeeds on the first try. Its JSON Schema
* keeps `format: "date-time"` and the field's description but not the regex, which every
* tools/list call would resend.
*/
function withoutPatternInJsonSchema<T extends z.ZodType>(schema: T): T {
return schema.meta({ pattern: undefined })
function mcpDateFilter(field: z.ZodOptional<z.ZodString>) {
return z.iso
.datetime({ offset: true, local: true })
.transform((value) => (HAS_UTC_OFFSET.test(value) ? value : `${value}Z`))
.optional()
.meta({ pattern: undefined, description: field.description })
}

const { startDate, endDate, modifiedAfter, modifiedBefore } = workspaceSearchFiltersSchema.shape

/** The date filters of the Search MCP tools, as their tools/list schema presents them. */
const mcpDateFilterFields = {
startDate: withoutPatternInJsonSchema(startDate),
endDate: withoutPatternInJsonSchema(endDate),
modifiedAfter: withoutPatternInJsonSchema(modifiedAfter),
modifiedBefore: withoutPatternInJsonSchema(modifiedBefore),
startDate: mcpDateFilter(startDate),
endDate: mcpDateFilter(endDate),
modifiedAfter: mcpDateFilter(modifiedAfter),
modifiedBefore: mcpDateFilter(modifiedBefore),
}

export const liveSearchMcpSchema = searchWorkspaceInputFieldsSchema
Expand Down
20 changes: 20 additions & 0 deletions apps/sim/lib/knowledge/mcp/server.protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,26 @@ describe('Search MCP protocol', () => {
expect(read.content).toEqual([
{ type: 'text', text: expect.stringContaining('Read evidence') },
])
const offsetless = await client.callTool({
name: 'search',
arguments: {
query: 'release',
startDate: '2026-09-28T00:00:00',
endDate: '2026-10-08T00:00',
modifiedAfter: '2026-09-01T08:00:00-07:00',
},
})
expect(offsetless.isError).not.toBe(true)
expect(mocks.liveSearch.mock.lastCall?.[0].input.filters).toMatchObject({
startDate: '2026-09-28T00:00:00Z',
endDate: '2026-10-08T00:00Z',
modifiedAfter: '2026-09-01T08:00:00-07:00',
})
const notADate = await client.callTool({
name: 'search',
arguments: { query: 'release', startDate: '2026-09-28' },
})
expect(notADate.isError).toBe(true)
} finally {
await client.close()
await server.close()
Expand Down
Loading