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
10 changes: 7 additions & 3 deletions apps/sim/lib/api/contracts/knowledge/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,23 @@ export function liveSearchMcpDefaultTopK(input: z.output<typeof liveSearchMcpSch
/** Documents one read_document call may read; each counts as one read against the rate limit. */
const MAX_MCP_READ_DOCUMENTS = 10

const mcpDocumentIdSchema = readDocumentInputSchema.shape.documentId.describe(
'A document ID returned by search, or a link to the document in its source.'
)

export const readLiveDocumentMcpSchema = readDocumentInputSchema
.extend({
documentId: readDocumentInputSchema.shape.documentId.optional(),
documentId: mcpDocumentIdSchema.optional(),
startChunkIndex: readDocumentInputSchema.shape.startChunkIndex.describe(
'Copy next.startChunkIndex from a prior read to continue it; live documents always use 0.'
),
documentIds: z
.array(readDocumentInputSchema.shape.documentId)
.array(mcpDocumentIdSchema)
.min(1)
.max(MAX_MCP_READ_DOCUMENTS)
.optional()
.describe(
`Up to ${MAX_MCP_READ_DOCUMENTS} document IDs returned by search, read together from the beginning. Use instead of documentId.`
`Up to ${MAX_MCP_READ_DOCUMENTS} document IDs or links, read together from the beginning. Use instead of documentId.`
),
})
.strict()
Expand Down
2 changes: 1 addition & 1 deletion apps/sim/lib/knowledge/mcp/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export function createKnowledgeMcpServer(context: KnowledgeMcpContext): McpServe
{
title: 'Read document',
description:
'Read live documents using the exact document IDs returned by search: documentId for one, or documentIds to read several in one call rather than one call each. Access and the admin’s source settings are checked again on every read. When a document’s hasMore is true, pass its next.startChunkIndex and next.startOffset with the same documentId to continue. Cite citationUrl when available.',
'Read live documents using the exact document IDs returned by search: documentId for one, or documentIds to read several in one call rather than one call each. A link the user shares to a Google Drive, Gmail, Calendar, Slack, Jira, Confluence, GitHub, GitLab, Linear, Notion or Fireflies item reads the same way, without searching for it first. Access and the admin’s source settings are checked again on every read. When a document’s hasMore is true, pass its next.startChunkIndex and next.startOffset with the returned documentId to continue. Cite citationUrl when available.',
inputSchema: readLiveDocumentMcpSchema,
annotations: KNOWLEDGE_MCP_READ_ONLY,
},
Expand Down
2 changes: 2 additions & 0 deletions apps/sim/lib/knowledge/search/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ export interface SearchDiagnosticMetadata {
dateWindowDays?: number
/** One entry per native query, in request order, so `queryIndex` resolves to a provider. */
nativeQueries?: NativeQueryShape[]
/** Whether a read named its document by a pasted source link rather than a document ID. */
fromLink?: boolean
/** The source a read came from; a search reports its sources on each provider summary. */
provider?: LiveSearchProvider
/** The provider's type for the document read, such as `message` or `file`. */
Expand Down
12 changes: 12 additions & 0 deletions apps/sim/lib/sim-search/live/account-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
withRequestSignal,
} from '@/lib/sim-search/live/http'
import { readHubSpotMcp, searchHubSpotMcp } from '@/lib/sim-search/live/hubspot-mcp'
import { type DocumentLink, locateDocumentLink } from '@/lib/sim-search/live/links'
import { readLucidMcp, searchLucidMcp } from '@/lib/sim-search/live/lucid-mcp'
import { createManagedSearchMcpClient } from '@/lib/sim-search/live/managed-mcp'
import { isManagedSearchMcpProvider } from '@/lib/sim-search/live/managed-mcp-config'
Expand Down Expand Up @@ -53,6 +54,12 @@ export interface LiveAccountSession {
/** True only when the document is inside the source boundary and the member may read it. */
verify(document: Reference): Promise<boolean>
read(reference: Reference, filters?: WorkspaceSearchFilters): Promise<NativeDocument>
/**
* The reference a pasted link names within this account, bound like a search result's, or
* undefined when the link names another provider, site, host or workspace. Not a permission
* check: a located reference is verified like any other before it is read.
*/
locate(link: DocumentLink): Promise<Reference | undefined>
/**
* Full text of a search result whose provider returned only an excerpt or metadata; `signal`
* cancels the read when its preview is no longer wanted.
Expand Down Expand Up @@ -254,6 +261,11 @@ export async function openLiveAccountSession(
)
return readMcp(reference)
},
async locate(link) {
if (link.provider !== provider) return undefined
const located = await locateDocumentLink(link, { client, origin })
return located && (connectionBinding ? { ...located, connectionBinding } : located)
},
async previewText(document, previewSignal) {
if (admin || !client) return undefined
return previewNativeText(provider, withRequestSignal(client, previewSignal), document)
Expand Down
116 changes: 116 additions & 0 deletions apps/sim/lib/sim-search/live/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,122 @@ describe('authorized live retrieval', () => {
).rejects.toMatchObject({ message: expect.stringContaining('next.startOffset') })
expect(mocks.read).not.toHaveBeenCalled()
})
describe('pasted links', () => {
const owner = liveReferenceOwner(
'reader',
resourceScopeKey(resourceScopeFromOwner({ workspaceId: 'workspace' }))
)
const read = (documentId: string) =>
readLiveDocument.execute({
principal,
input: {
workspaceId: 'workspace',
documentId,
limit: 3,
resultSecretRegistry: new ResolvedSecretTraceRegistry([]),
},
})
const confluence = (id: string) => ({
...account,
id,
provider: 'confluence',
providerId: 'confluence',
displayName: id,
})

it('reads a Drive link and names it by the ID search mints, which reads back', async () => {
mocks.read.mockResolvedValue({
...document,
id: '1AbcDEFghijKLmn',
kind: 'application/vnd.google-apps.document',
})
const result = await read('https://drive.google.com/file/d/1AbcDEFghijKLmn/view')
expect(mocks.read).toHaveBeenCalledWith(
'google_drive',
expect.anything(),
{ id: '1AbcDEFghijKLmn' },
expect.anything()
)
expect(decodeLiveReference(result.documentId)).toEqual({
account: 'account',
provider: 'google_drive',
id: '1AbcDEFghijKLmn',
kind: 'application/vnd.google-apps.document',
})
expect(result.chunks[0]?.content).toBe('Evidence')
await expect(read(result.documentId)).resolves.toMatchObject({
documentId: result.documentId,
})
})

it('verifies a linked document before reading it, like a document ID', async () => {
mocks.service.mockResolvedValue({
policy: defaultLiveSearchPolicy(),
verify: vi.fn(async () => false),
partial: false,
})
await expect(read('https://drive.google.com/file/d/1AbcDEFghijKLmn/view')).rejects.toThrow(
'outside your organization'
)
expect(mocks.read).not.toHaveBeenCalled()
})

it('reads an Atlassian link only from the account on its site', async () => {
mocks.accounts.mockResolvedValue([confluence('site-a'), confluence('site-b')])
mocks.json
.mockResolvedValueOnce([{ id: 'cloud-a', url: 'https://a.atlassian.net' }])
.mockResolvedValueOnce([{ id: 'cloud-b', url: 'https://b.atlassian.net' }])
mocks.read.mockResolvedValue({ ...document, id: '12345', kind: 'page', container: 'cloud-b' })
const result = await read('https://b.atlassian.net/wiki/spaces/ENG/pages/12345/Launch')
expect(mocks.read).toHaveBeenCalledOnce()
expect(mocks.read).toHaveBeenCalledWith(
'confluence',
expect.anything(),
{ id: '12345', kind: 'page', container: 'cloud-b' },
expect.anything()
)
expect(result.documentId).toBe(
encodeLiveReference(
{
account: 'site-b',
provider: 'confluence',
id: '12345',
kind: 'page',
container: 'cloud-b',
},
owner
)
)
})

it('reports one account’s specific reason over another account not finding the link', async () => {
mocks.accounts.mockResolvedValue([confluence('site-a'), confluence('site-a-2')])
mocks.json
.mockResolvedValueOnce([{ id: 'cloud-a', url: 'https://a.atlassian.net' }])
.mockResolvedValueOnce([{ id: 'cloud-a', url: 'https://a.atlassian.net' }])
mocks.read
.mockRejectedValueOnce(new NativeSearchError('unavailable', 'Gone', undefined, 404))
.mockRejectedValueOnce(new NativeSearchError('reconnect', 'The provider denied access.'))
await expect(read('https://a.atlassian.net/wiki/spaces/ENG/pages/7')).rejects.toMatchObject({
code: 'unauthorized',
message: expect.stringContaining('Reconnect Confluence'),
})
})

it('names the source to connect when no account can open the link', async () => {
await expect(read('https://acme.slack.com/archives/C0123/p1700000000123456')).rejects.toThrow(
'No connected Slack account'
)
expect(mocks.resolveAccount).not.toHaveBeenCalled()
})

it('refuses a link it cannot read before listing any account', async () => {
await expect(read('https://example.com/about')).rejects.toMatchObject({
code: 'validation',
})
expect(mocks.accounts).not.toHaveBeenCalled()
})
})
describe('live document IDs', () => {
const workspaceScope = resourceScopeKey(resourceScopeFromOwner({ workspaceId: 'workspace' }))
/** The earlier wire form, spelled out so these tests pin what older conversations hold. */
Expand Down
Loading
Loading