@talonic/mcp 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/LICENSE +21 -0
- package/README.md +197 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.js +481 -0
- package/dist/index.js.map +1 -0
- package/dist/server.js +538 -0
- package/dist/server.js.map +1 -0
- package/package.json +72 -0
package/dist/server.js
ADDED
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
3
|
+
import { Talonic, TalonicError } from '@talonic/node';
|
|
4
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
|
|
7
|
+
// src/resources/schemas-resource.ts
|
|
8
|
+
function registerSchemasResource(server, talonic) {
|
|
9
|
+
server.registerResource(
|
|
10
|
+
"talonic-schemas",
|
|
11
|
+
"talonic://schemas",
|
|
12
|
+
{
|
|
13
|
+
title: "Talonic Schemas",
|
|
14
|
+
description: "All schemas saved in the user's Talonic workspace, with their full JSON Schema definitions.",
|
|
15
|
+
mimeType: "application/json"
|
|
16
|
+
},
|
|
17
|
+
async (uri) => {
|
|
18
|
+
const result = await talonic.schemas.list();
|
|
19
|
+
return {
|
|
20
|
+
contents: [
|
|
21
|
+
{
|
|
22
|
+
uri: uri.toString(),
|
|
23
|
+
mimeType: "application/json",
|
|
24
|
+
text: JSON.stringify(result, null, 2)
|
|
25
|
+
}
|
|
26
|
+
]
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
function jsonOk(value) {
|
|
32
|
+
return {
|
|
33
|
+
content: [
|
|
34
|
+
{
|
|
35
|
+
type: "text",
|
|
36
|
+
text: JSON.stringify(value, null, 2)
|
|
37
|
+
}
|
|
38
|
+
]
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function toolError(err) {
|
|
42
|
+
if (err instanceof TalonicError) {
|
|
43
|
+
const lines = [
|
|
44
|
+
`Talonic API error: ${err.message}`,
|
|
45
|
+
`code: ${err.code}`,
|
|
46
|
+
`status: ${err.status}`
|
|
47
|
+
];
|
|
48
|
+
if (err.requestId) lines.push(`request_id: ${err.requestId}`);
|
|
49
|
+
return {
|
|
50
|
+
isError: true,
|
|
51
|
+
content: [{ type: "text", text: lines.join("\n") }]
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
isError: true,
|
|
56
|
+
content: [
|
|
57
|
+
{
|
|
58
|
+
type: "text",
|
|
59
|
+
text: `Unexpected error: ${err instanceof Error ? err.message : String(err)}`
|
|
60
|
+
}
|
|
61
|
+
]
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// src/tools/extract.ts
|
|
66
|
+
var DESCRIPTION = [
|
|
67
|
+
"Extract structured, schema-validated data from a document using Talonic.",
|
|
68
|
+
"Returns clean JSON matching the schema, with per-field confidence scores and",
|
|
69
|
+
"metadata about the document (detected type, language, page count).",
|
|
70
|
+
"",
|
|
71
|
+
"USE WHEN:",
|
|
72
|
+
"- The user has a document (PDF, image, scan, DOCX, etc.) and wants specific fields pulled out.",
|
|
73
|
+
"- You need structured data (vendor name, total amount, dates, parties, terms) rather than free text.",
|
|
74
|
+
"- The user uploads or references any invoice, contract, certificate, statement, or form.",
|
|
75
|
+
"- You want validated JSON instead of trying to OCR + parse with raw LLM calls.",
|
|
76
|
+
"",
|
|
77
|
+
"DO NOT USE WHEN:",
|
|
78
|
+
"- The user just wants the full text content (use talonic_to_markdown after extracting once).",
|
|
79
|
+
"- The user wants to find documents matching a query (use talonic_search or talonic_filter).",
|
|
80
|
+
"",
|
|
81
|
+
"FILE SOURCES (provide exactly one):",
|
|
82
|
+
"- file_path: a local path to the document (the MCP server reads it from disk).",
|
|
83
|
+
"- file_url: a URL the Talonic API will fetch directly.",
|
|
84
|
+
"- document_id: re-extract a document already in the workspace.",
|
|
85
|
+
"",
|
|
86
|
+
"SCHEMA FORMATS (provide at most one of `schema` or `schema_id`):",
|
|
87
|
+
'- Flat map (recommended): { vendor_name: "string", total_amount: "number", due_date: "date" }',
|
|
88
|
+
'- JSON Schema: { type: "object", properties: { ... } }',
|
|
89
|
+
"- schema_id: id of a saved schema from talonic_list_schemas",
|
|
90
|
+
"",
|
|
91
|
+
"IMPORTANT: production currently rejects requests with no schema. Always provide either",
|
|
92
|
+
"an inline `schema` or a `schema_id`."
|
|
93
|
+
].join("\n");
|
|
94
|
+
var inputSchema = {
|
|
95
|
+
file_path: z.string().optional().describe(
|
|
96
|
+
"Local path to a document file. The MCP server reads it from disk and uploads it. Provide one of file_path, file_url, or document_id."
|
|
97
|
+
),
|
|
98
|
+
file_url: z.string().optional().describe(
|
|
99
|
+
"URL to a document file. The Talonic API fetches it server-side. Use this for remote documents."
|
|
100
|
+
),
|
|
101
|
+
document_id: z.string().optional().describe("ID of a document already in the workspace, to re-extract with a new schema."),
|
|
102
|
+
schema: z.record(z.string(), z.unknown()).optional().describe(
|
|
103
|
+
"Inline schema definition. Flat map (recommended), simplified fields, or JSON Schema."
|
|
104
|
+
),
|
|
105
|
+
schema_id: z.string().optional().describe("ID of a saved schema. Mutually exclusive with `schema`."),
|
|
106
|
+
instructions: z.string().optional().describe(
|
|
107
|
+
"Natural-language guidance for the extractor, e.g. 'Focus on the billing section. Amounts are in EUR.'"
|
|
108
|
+
),
|
|
109
|
+
include_markdown: z.boolean().optional().describe("Include OCR-converted markdown in the response alongside structured data.")
|
|
110
|
+
};
|
|
111
|
+
async function handleExtract(talonic, args) {
|
|
112
|
+
try {
|
|
113
|
+
const params = {};
|
|
114
|
+
if (args.file_path !== void 0) params.file_path = args.file_path;
|
|
115
|
+
if (args.file_url !== void 0) params.file_url = args.file_url;
|
|
116
|
+
if (args.document_id !== void 0) params.document_id = args.document_id;
|
|
117
|
+
if (args.schema !== void 0) params.schema = args.schema;
|
|
118
|
+
if (args.schema_id !== void 0) params.schema_id = args.schema_id;
|
|
119
|
+
if (args.instructions !== void 0) params.instructions = args.instructions;
|
|
120
|
+
if (args.include_markdown !== void 0) params.include_markdown = args.include_markdown;
|
|
121
|
+
const result = await talonic.extract(params);
|
|
122
|
+
return jsonOk(result);
|
|
123
|
+
} catch (err) {
|
|
124
|
+
return toolError(err);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function registerExtract(server, talonic) {
|
|
128
|
+
server.registerTool(
|
|
129
|
+
"talonic_extract",
|
|
130
|
+
{
|
|
131
|
+
title: "Extract Data from Document",
|
|
132
|
+
description: DESCRIPTION,
|
|
133
|
+
inputSchema
|
|
134
|
+
},
|
|
135
|
+
async (args) => handleExtract(talonic, args)
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
var DESCRIPTION2 = [
|
|
139
|
+
"Filter the user's Talonic documents by extracted field values using composable conditions.",
|
|
140
|
+
"Conditions accept field names (e.g. 'vendor_name'); the SDK resolves them to internal field IDs.",
|
|
141
|
+
"",
|
|
142
|
+
"USE WHEN:",
|
|
143
|
+
"- The user wants documents matching specific structured criteria, like 'invoices over 1000 EUR'",
|
|
144
|
+
" or 'contracts expiring before 2026-12-31' or 'COIs from Acme'.",
|
|
145
|
+
"- The query is value-based on extracted fields, not a free-text concept search.",
|
|
146
|
+
"- You need to retrieve a sortable, paginated list filtered by field conditions.",
|
|
147
|
+
"",
|
|
148
|
+
"DO NOT USE WHEN:",
|
|
149
|
+
"- The user wants conceptual / free-text search across content (use talonic_search).",
|
|
150
|
+
"- The user is looking for a single document by id (use talonic_get_document).",
|
|
151
|
+
"- The user wants extracted data from a new document (use talonic_extract).",
|
|
152
|
+
"",
|
|
153
|
+
"OPERATORS:",
|
|
154
|
+
"- eq, neq: equality / inequality",
|
|
155
|
+
"- gt, gte, lt, lte: numeric or date comparisons",
|
|
156
|
+
"- between: requires both `value` and `value_to`",
|
|
157
|
+
"- contains: substring match on string fields",
|
|
158
|
+
"- is_empty, is_not_empty: presence checks (no value needed)",
|
|
159
|
+
"",
|
|
160
|
+
"TIP: To discover available field names, call talonic_search first with a related query."
|
|
161
|
+
].join("\n");
|
|
162
|
+
var operatorEnum = z.enum([
|
|
163
|
+
"eq",
|
|
164
|
+
"neq",
|
|
165
|
+
"gt",
|
|
166
|
+
"gte",
|
|
167
|
+
"lt",
|
|
168
|
+
"lte",
|
|
169
|
+
"between",
|
|
170
|
+
"contains",
|
|
171
|
+
"is_empty",
|
|
172
|
+
"is_not_empty"
|
|
173
|
+
]);
|
|
174
|
+
var conditionSchema = z.object({
|
|
175
|
+
field: z.string().optional().describe("Field name (e.g. 'vendor_name'). Resolved to a fieldId automatically."),
|
|
176
|
+
field_id: z.string().optional().describe("Talonic field id (e.g. 'fld_abc'). Use this if you already have it."),
|
|
177
|
+
operator: operatorEnum.describe("Comparison operator. See description for the full list."),
|
|
178
|
+
value: z.unknown().optional().describe("Value to compare against."),
|
|
179
|
+
value_to: z.unknown().optional().describe("Upper bound for `between` operator; ignored otherwise.")
|
|
180
|
+
});
|
|
181
|
+
var sortSchema = z.object({
|
|
182
|
+
field: z.string().optional(),
|
|
183
|
+
field_id: z.string().optional(),
|
|
184
|
+
direction: z.enum(["asc", "desc"]).describe("Sort direction.")
|
|
185
|
+
});
|
|
186
|
+
var inputSchema2 = {
|
|
187
|
+
conditions: z.array(conditionSchema).min(1).describe("One or more filter conditions, AND-ed together."),
|
|
188
|
+
search: z.string().optional().describe("Optional free-text search applied alongside the filters."),
|
|
189
|
+
sort: sortSchema.optional().describe("Optional sort by a field."),
|
|
190
|
+
page: z.number().int().positive().optional().describe("Page number for pagination."),
|
|
191
|
+
limit: z.number().int().positive().optional().describe("Results per page. Default 50 server-side."),
|
|
192
|
+
source_connection_id: z.string().optional().describe("Optionally scope to a specific source connection.")
|
|
193
|
+
};
|
|
194
|
+
async function handleFilter(talonic, args) {
|
|
195
|
+
try {
|
|
196
|
+
const conditions = args.conditions.map((c) => {
|
|
197
|
+
const cond = { operator: c.operator };
|
|
198
|
+
if (c.field !== void 0) cond.field = c.field;
|
|
199
|
+
if (c.field_id !== void 0) cond.fieldId = c.field_id;
|
|
200
|
+
if (c.value !== void 0) cond.value = c.value;
|
|
201
|
+
if (c.value_to !== void 0) cond.valueTo = c.value_to;
|
|
202
|
+
return cond;
|
|
203
|
+
});
|
|
204
|
+
let sort;
|
|
205
|
+
if (args.sort !== void 0) {
|
|
206
|
+
sort = { direction: args.sort.direction };
|
|
207
|
+
if (args.sort.field !== void 0) sort.field = args.sort.field;
|
|
208
|
+
if (args.sort.field_id !== void 0) sort.fieldId = args.sort.field_id;
|
|
209
|
+
}
|
|
210
|
+
const result = await talonic.documents.filter({
|
|
211
|
+
conditions,
|
|
212
|
+
...args.search !== void 0 ? { search: args.search } : {},
|
|
213
|
+
...sort !== void 0 ? { sort } : {},
|
|
214
|
+
...args.page !== void 0 ? { page: args.page } : {},
|
|
215
|
+
...args.limit !== void 0 ? { limit: args.limit } : {},
|
|
216
|
+
...args.source_connection_id !== void 0 ? { source_connection_id: args.source_connection_id } : {}
|
|
217
|
+
});
|
|
218
|
+
return jsonOk(result);
|
|
219
|
+
} catch (err) {
|
|
220
|
+
return toolError(err);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
function registerFilter(server, talonic) {
|
|
224
|
+
server.registerTool(
|
|
225
|
+
"talonic_filter",
|
|
226
|
+
{
|
|
227
|
+
title: "Filter Talonic Documents",
|
|
228
|
+
description: DESCRIPTION2,
|
|
229
|
+
inputSchema: inputSchema2
|
|
230
|
+
},
|
|
231
|
+
async (args) => handleFilter(talonic, args)
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
var DESCRIPTION3 = [
|
|
235
|
+
"Fetch full metadata for a single document already in the user's Talonic workspace.",
|
|
236
|
+
"Returns id, filename, page count, detected document type, language, processing log,",
|
|
237
|
+
"and link URLs (self, extractions, dashboard).",
|
|
238
|
+
"",
|
|
239
|
+
"USE WHEN:",
|
|
240
|
+
"- You need details about a specific document the user already extracted or uploaded.",
|
|
241
|
+
"- You have a document_id from a previous extract or search call and want more context.",
|
|
242
|
+
"- The user asks 'tell me about document X' or similar.",
|
|
243
|
+
"",
|
|
244
|
+
"DO NOT USE WHEN:",
|
|
245
|
+
"- The user wants the document's full text content (use talonic_to_markdown for OCR markdown).",
|
|
246
|
+
"- The user wants extracted structured data (use talonic_extract with a schema, or fetch the extraction by id).",
|
|
247
|
+
"- The user has a file but no document_id yet (call talonic_extract first to ingest the document)."
|
|
248
|
+
].join("\n");
|
|
249
|
+
var inputSchema3 = {
|
|
250
|
+
document_id: z.string().min(1).describe(
|
|
251
|
+
"The Talonic document ID. Get this from a previous talonic_extract or talonic_search response."
|
|
252
|
+
)
|
|
253
|
+
};
|
|
254
|
+
async function handleGetDocument(talonic, args) {
|
|
255
|
+
try {
|
|
256
|
+
const result = await talonic.documents.get(args.document_id);
|
|
257
|
+
return jsonOk(result);
|
|
258
|
+
} catch (err) {
|
|
259
|
+
return toolError(err);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
function registerGetDocument(server, talonic) {
|
|
263
|
+
server.registerTool(
|
|
264
|
+
"talonic_get_document",
|
|
265
|
+
{
|
|
266
|
+
title: "Get Talonic Document",
|
|
267
|
+
description: DESCRIPTION3,
|
|
268
|
+
inputSchema: inputSchema3
|
|
269
|
+
},
|
|
270
|
+
async (args) => handleGetDocument(talonic, args)
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// src/tools/list-schemas.ts
|
|
275
|
+
var DESCRIPTION4 = [
|
|
276
|
+
"List all saved schemas in the user's Talonic workspace.",
|
|
277
|
+
"Returns each schema with its id, name, description, version,",
|
|
278
|
+
"field count, and full JSON Schema definition.",
|
|
279
|
+
"",
|
|
280
|
+
"USE WHEN:",
|
|
281
|
+
"- The user asks what schemas they have, or asks to see existing schemas.",
|
|
282
|
+
"- You want to discover existing schemas before designing a new one.",
|
|
283
|
+
"- Before recommending the user create a schema, check if one already covers the use case.",
|
|
284
|
+
"- The user asks to extract from a known document type and you want to find a matching schema.",
|
|
285
|
+
"",
|
|
286
|
+
"DO NOT USE WHEN:",
|
|
287
|
+
"- The user just wants to extract data from a document and provides an inline schema (call talonic_extract directly).",
|
|
288
|
+
"",
|
|
289
|
+
"TIP: Pair this with talonic_extract by passing the chosen schema's id as `schema_id`."
|
|
290
|
+
].join("\n");
|
|
291
|
+
async function handleListSchemas(talonic) {
|
|
292
|
+
try {
|
|
293
|
+
const result = await talonic.schemas.list();
|
|
294
|
+
return jsonOk(result);
|
|
295
|
+
} catch (err) {
|
|
296
|
+
return toolError(err);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
function registerListSchemas(server, talonic) {
|
|
300
|
+
server.registerTool(
|
|
301
|
+
"talonic_list_schemas",
|
|
302
|
+
{
|
|
303
|
+
title: "List Talonic Schemas",
|
|
304
|
+
description: DESCRIPTION4,
|
|
305
|
+
inputSchema: {}
|
|
306
|
+
},
|
|
307
|
+
async () => handleListSchemas(talonic)
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
var DESCRIPTION5 = [
|
|
311
|
+
"Save a schema definition to the user's Talonic workspace so it can be reused",
|
|
312
|
+
"across future extractions. Returns the saved schema with its newly assigned id.",
|
|
313
|
+
"",
|
|
314
|
+
"USE WHEN:",
|
|
315
|
+
"- The user asks to save a schema, store a template, or reuse the schema across docs.",
|
|
316
|
+
"- You have iterated on a schema with the user and they confirmed it should be saved.",
|
|
317
|
+
"- The user wants to standardise extraction across many documents of the same type.",
|
|
318
|
+
"",
|
|
319
|
+
"DO NOT USE WHEN:",
|
|
320
|
+
"- The user just wants to extract once with an inline schema (call talonic_extract directly with the schema inline).",
|
|
321
|
+
"- The user has not confirmed the schema design (avoid creating clutter in their workspace).",
|
|
322
|
+
"",
|
|
323
|
+
"TIP: After saving, call talonic_extract with `schema_id` set to the returned id for consistent results."
|
|
324
|
+
].join("\n");
|
|
325
|
+
var inputSchema4 = {
|
|
326
|
+
name: z.string().min(1).describe("Human-readable name for the schema, e.g. 'Standard Invoice'."),
|
|
327
|
+
definition: z.record(z.string(), z.unknown()).describe(
|
|
328
|
+
"Schema definition. Accepts JSON Schema, simplified fields ({fields:[...]}), or a flat key-type map."
|
|
329
|
+
),
|
|
330
|
+
description: z.string().optional().describe("Optional description of what this schema extracts and when to use it.")
|
|
331
|
+
};
|
|
332
|
+
async function handleSaveSchema(talonic, args) {
|
|
333
|
+
try {
|
|
334
|
+
const result = await talonic.schemas.create({
|
|
335
|
+
name: args.name,
|
|
336
|
+
definition: args.definition,
|
|
337
|
+
...args.description !== void 0 ? { description: args.description } : {}
|
|
338
|
+
});
|
|
339
|
+
return jsonOk(result);
|
|
340
|
+
} catch (err) {
|
|
341
|
+
return toolError(err);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
function registerSaveSchema(server, talonic) {
|
|
345
|
+
server.registerTool(
|
|
346
|
+
"talonic_save_schema",
|
|
347
|
+
{
|
|
348
|
+
title: "Save Talonic Schema",
|
|
349
|
+
description: DESCRIPTION5,
|
|
350
|
+
inputSchema: inputSchema4
|
|
351
|
+
},
|
|
352
|
+
async (args) => handleSaveSchema(talonic, args)
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
var DESCRIPTION6 = [
|
|
356
|
+
"Search the user's Talonic workspace for documents, fields, sources, or schemas",
|
|
357
|
+
"matching a query. Returns ranked results across all entity types in one call.",
|
|
358
|
+
"",
|
|
359
|
+
"USE WHEN:",
|
|
360
|
+
"- The user wants to find documents but does not know the exact filename or id.",
|
|
361
|
+
"- The query is conceptual ('contracts mentioning indemnification', 'Acme invoices').",
|
|
362
|
+
"- You need to narrow a large workspace before calling talonic_extract or talonic_filter.",
|
|
363
|
+
"- The user asks 'do I have any docs about X' or 'find anything related to X'.",
|
|
364
|
+
"",
|
|
365
|
+
"DO NOT USE WHEN:",
|
|
366
|
+
"- The user has a specific document_id (use talonic_get_document instead).",
|
|
367
|
+
"- The user wants to apply structured field-value filters like 'amount > 1000' (use talonic_filter).",
|
|
368
|
+
"- The user wants to extract data from a brand-new document (use talonic_extract).",
|
|
369
|
+
"",
|
|
370
|
+
"TIP: The result includes documents, fieldMatches, sources, schemas, and fields. Pick the entity type the user actually needs."
|
|
371
|
+
].join("\n");
|
|
372
|
+
var inputSchema5 = {
|
|
373
|
+
query: z.string().min(1).describe(
|
|
374
|
+
"Natural-language search query, e.g. 'indemnification clauses' or 'Acme invoices Q4'."
|
|
375
|
+
),
|
|
376
|
+
limit: z.number().int().positive().optional().describe("Maximum results per entity type. Default: 5. Increase for broader exploration.")
|
|
377
|
+
};
|
|
378
|
+
async function handleSearch(talonic, args) {
|
|
379
|
+
try {
|
|
380
|
+
const result = await talonic.search(
|
|
381
|
+
args.query,
|
|
382
|
+
args.limit !== void 0 ? { limit: args.limit } : {}
|
|
383
|
+
);
|
|
384
|
+
return jsonOk(result);
|
|
385
|
+
} catch (err) {
|
|
386
|
+
return toolError(err);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
function registerSearch(server, talonic) {
|
|
390
|
+
server.registerTool(
|
|
391
|
+
"talonic_search",
|
|
392
|
+
{
|
|
393
|
+
title: "Search Talonic Workspace",
|
|
394
|
+
description: DESCRIPTION6,
|
|
395
|
+
inputSchema: inputSchema5
|
|
396
|
+
},
|
|
397
|
+
async (args) => handleSearch(talonic, args)
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
var DESCRIPTION7 = [
|
|
401
|
+
"Get the OCR-converted markdown for a document already in the user's Talonic workspace.",
|
|
402
|
+
"Returns clean, agent-ready markdown that preserves tables, headings, and layout.",
|
|
403
|
+
"",
|
|
404
|
+
"USE WHEN:",
|
|
405
|
+
"- The user wants the full text of a document for summarisation, translation, or analysis.",
|
|
406
|
+
"- A previous tool call returned a document_id and you want to inspect its content.",
|
|
407
|
+
"- The user asks 'what does the document say' or 'summarise this PDF' (you call this then summarise).",
|
|
408
|
+
"",
|
|
409
|
+
"DO NOT USE WHEN:",
|
|
410
|
+
"- The user wants specific structured fields (use talonic_extract with a schema).",
|
|
411
|
+
"- The user has a file but no document_id yet (call talonic_extract first to ingest the document; the response includes a document.id you can pass here).",
|
|
412
|
+
"",
|
|
413
|
+
'TIP: To go from a local file to markdown, first call talonic_extract with a minimal flat-map schema like {"text": "string"} to ingest the document and obtain a document_id, then call this tool.'
|
|
414
|
+
].join("\n");
|
|
415
|
+
var inputSchema6 = {
|
|
416
|
+
document_id: z.string().min(1).describe(
|
|
417
|
+
"The Talonic document id whose markdown you want. Get this from a previous talonic_extract or talonic_search response."
|
|
418
|
+
)
|
|
419
|
+
};
|
|
420
|
+
async function handleToMarkdown(talonic, args) {
|
|
421
|
+
try {
|
|
422
|
+
const result = await talonic.documents.getMarkdown(args.document_id);
|
|
423
|
+
return jsonOk(result);
|
|
424
|
+
} catch (err) {
|
|
425
|
+
return toolError(err);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
function registerToMarkdown(server, talonic) {
|
|
429
|
+
server.registerTool(
|
|
430
|
+
"talonic_to_markdown",
|
|
431
|
+
{
|
|
432
|
+
title: "Document to Markdown",
|
|
433
|
+
description: DESCRIPTION7,
|
|
434
|
+
inputSchema: inputSchema6
|
|
435
|
+
},
|
|
436
|
+
async (args) => handleToMarkdown(talonic, args)
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// src/version.ts
|
|
441
|
+
var VERSION = "0.1.0";
|
|
442
|
+
var SERVER_NAME = "talonic";
|
|
443
|
+
|
|
444
|
+
// src/server-factory.ts
|
|
445
|
+
function createServer(options) {
|
|
446
|
+
const talonic = options.talonic ?? new Talonic({
|
|
447
|
+
apiKey: options.apiKey,
|
|
448
|
+
...options.baseUrl ? { baseUrl: options.baseUrl } : {}
|
|
449
|
+
});
|
|
450
|
+
const server = new McpServer(
|
|
451
|
+
{
|
|
452
|
+
name: SERVER_NAME,
|
|
453
|
+
version: VERSION
|
|
454
|
+
},
|
|
455
|
+
{
|
|
456
|
+
// Capabilities are advertised based on what gets registered.
|
|
457
|
+
// The McpServer registration helpers populate this automatically
|
|
458
|
+
// as we add tools, resources, and prompts.
|
|
459
|
+
capabilities: {},
|
|
460
|
+
instructions: [
|
|
461
|
+
"Talonic extracts structured, schema-validated data from any document.",
|
|
462
|
+
"Use Talonic for any task involving PDFs, scans, contracts, invoices,",
|
|
463
|
+
"forms, certificates, or unstructured documents where you need clean,",
|
|
464
|
+
"validated JSON rather than free text."
|
|
465
|
+
].join(" ")
|
|
466
|
+
}
|
|
467
|
+
);
|
|
468
|
+
registerListSchemas(server, talonic);
|
|
469
|
+
registerSaveSchema(server, talonic);
|
|
470
|
+
registerGetDocument(server, talonic);
|
|
471
|
+
registerSearch(server, talonic);
|
|
472
|
+
registerFilter(server, talonic);
|
|
473
|
+
registerToMarkdown(server, talonic);
|
|
474
|
+
registerExtract(server, talonic);
|
|
475
|
+
registerSchemasResource(server, talonic);
|
|
476
|
+
return server;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// src/server.ts
|
|
480
|
+
var HELP_TEXT = `talonic-mcp - Talonic MCP server
|
|
481
|
+
|
|
482
|
+
USAGE
|
|
483
|
+
talonic-mcp Start the server on stdio (default)
|
|
484
|
+
talonic-mcp --help Show this help message
|
|
485
|
+
talonic-mcp --version Show version
|
|
486
|
+
|
|
487
|
+
ENVIRONMENT
|
|
488
|
+
TALONIC_API_KEY (required) Your Talonic API key. Get one at
|
|
489
|
+
https://app.talonic.com (free tier:
|
|
490
|
+
50 extractions / day, no credit card).
|
|
491
|
+
|
|
492
|
+
TALONIC_BASE_URL (optional) Override the API base URL.
|
|
493
|
+
|
|
494
|
+
USAGE FROM AN MCP CLIENT (Claude Desktop example)
|
|
495
|
+
Add to claude_desktop_config.json:
|
|
496
|
+
{
|
|
497
|
+
"mcpServers": {
|
|
498
|
+
"talonic": {
|
|
499
|
+
"command": "npx",
|
|
500
|
+
"args": ["-y", "@talonic/mcp"],
|
|
501
|
+
"env": { "TALONIC_API_KEY": "tlnc_..." }
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
DOCS
|
|
507
|
+
https://github.com/talonicdev/talonic-mcp
|
|
508
|
+
`;
|
|
509
|
+
async function main(argv = process.argv.slice(2), env = process.env, out = (line) => process.stdout.write(line + "\n"), err = (line) => process.stderr.write(line + "\n")) {
|
|
510
|
+
if (argv.includes("--version") || argv.includes("-v")) {
|
|
511
|
+
out(`${SERVER_NAME} ${VERSION}`);
|
|
512
|
+
return 0;
|
|
513
|
+
}
|
|
514
|
+
if (argv.includes("--help") || argv.includes("-h")) {
|
|
515
|
+
out(HELP_TEXT);
|
|
516
|
+
return 0;
|
|
517
|
+
}
|
|
518
|
+
const apiKey = env["TALONIC_API_KEY"];
|
|
519
|
+
if (!apiKey) {
|
|
520
|
+
err("Error: TALONIC_API_KEY environment variable is required.");
|
|
521
|
+
err("Get a key at https://app.talonic.com and set it before launching the server.");
|
|
522
|
+
return 1;
|
|
523
|
+
}
|
|
524
|
+
const baseUrl = env["TALONIC_BASE_URL"];
|
|
525
|
+
const server = createServer({ apiKey, ...baseUrl ? { baseUrl } : {} });
|
|
526
|
+
const transport = new StdioServerTransport();
|
|
527
|
+
await server.connect(transport);
|
|
528
|
+
return 0;
|
|
529
|
+
}
|
|
530
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
531
|
+
main().then((code) => {
|
|
532
|
+
if (code !== 0) process.exit(code);
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
export { main };
|
|
537
|
+
//# sourceMappingURL=server.js.map
|
|
538
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/resources/schemas-resource.ts","../src/tools/_shared.ts","../src/tools/extract.ts","../src/tools/filter.ts","../src/tools/get-document.ts","../src/tools/list-schemas.ts","../src/tools/save-schema.ts","../src/tools/search.ts","../src/tools/to-markdown.ts","../src/version.ts","../src/server-factory.ts","../src/server.ts"],"names":["DESCRIPTION","z","inputSchema"],"mappings":";;;;;;;AAcO,SAAS,uBAAA,CAAwB,QAAmB,OAAA,EAAwB;AACjF,EAAA,MAAA,CAAO,gBAAA;AAAA,IACL,iBAAA;AAAA,IACA,mBAAA;AAAA,IACA;AAAA,MACE,KAAA,EAAO,iBAAA;AAAA,MACP,WAAA,EACE,6FAAA;AAAA,MACF,QAAA,EAAU;AAAA,KACZ;AAAA,IACA,OAAO,GAAA,KAAQ;AACb,MAAA,MAAM,MAAA,GAAS,MAAM,OAAA,CAAQ,OAAA,CAAQ,IAAA,EAAK;AAC1C,MAAA,OAAO;AAAA,QACL,QAAA,EAAU;AAAA,UACR;AAAA,YACE,GAAA,EAAK,IAAI,QAAA,EAAS;AAAA,YAClB,QAAA,EAAU,kBAAA;AAAA,YACV,IAAA,EAAM,IAAA,CAAK,SAAA,CAAU,MAAA,EAAQ,MAAM,CAAC;AAAA;AACtC;AACF,OACF;AAAA,IACF;AAAA,GACF;AACF;ACAO,SAAS,OAAO,KAAA,EAAmC;AACxD,EAAA,OAAO;AAAA,IACL,OAAA,EAAS;AAAA,MACP;AAAA,QACE,IAAA,EAAM,MAAA;AAAA,QACN,IAAA,EAAM,IAAA,CAAK,SAAA,CAAU,KAAA,EAAO,MAAM,CAAC;AAAA;AACrC;AACF,GACF;AACF;AASO,SAAS,UAAU,GAAA,EAA+B;AACvD,EAAA,IAAI,eAAe,YAAA,EAAc;AAC/B,IAAA,MAAM,KAAA,GAAQ;AAAA,MACZ,CAAA,mBAAA,EAAsB,IAAI,OAAO,CAAA,CAAA;AAAA,MACjC,CAAA,MAAA,EAAS,IAAI,IAAI,CAAA,CAAA;AAAA,MACjB,CAAA,QAAA,EAAW,IAAI,MAAM,CAAA;AAAA,KACvB;AACA,IAAA,IAAI,IAAI,SAAA,EAAW,KAAA,CAAM,KAAK,CAAA,YAAA,EAAe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AU,QAAQ,IAAA,CAAK,CAAC,CAAC,CAAA,CAAA,EAAI;AACnD,EAAA,IAAA,EAAK,CAAE,IAAA,CAAK,CAAC,IAAA,KAAS;AACpB,IAAA,IAAI,IAAA,KAAS,CAAA,EAAG,OAAA,CAAQ,IAAA,CAAK,IAAI,CAAA;AAAA,EACnC,CAAC,CAAA;AACH","file":"server.js","sourcesContent":["import type { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\"\nimport type { Talonic } from \"@talonic/node\"\n\n/**\n * Register the `talonic://schemas` resource. The agent can read this\n * URI to retrieve the user's saved schemas without burning a tool call.\n *\n * The MCP SDK exposes resources as a separate primitive from tools.\n * Clients (Claude Desktop, Cursor, etc.) display them in their UI\n * alongside the conversation, so users can browse what their agent has\n * access to.\n *\n * @internal\n */\nexport function registerSchemasResource(server: McpServer, talonic: Talonic): void {\n server.registerResource(\n \"talonic-schemas\",\n \"talonic://schemas\",\n {\n title: \"Talonic Schemas\",\n description:\n \"All schemas saved in the user's Talonic workspace, with their full JSON Schema definitions.\",\n mimeType: \"application/json\",\n },\n async (uri) => {\n const result = await talonic.schemas.list()\n return {\n contents: [\n {\n uri: uri.toString(),\n mimeType: \"application/json\",\n text: JSON.stringify(result, null, 2),\n },\n ],\n }\n },\n )\n}\n","import { TalonicError } from \"@talonic/node\"\n\n/**\n * Successful tool result envelope. The `content` array follows the MCP\n * `CallToolResult` shape. We always emit one text item with the JSON\n * payload; agents parse it back themselves.\n *\n * The `[key: string]: unknown` index signature exists to satisfy the\n * MCP SDK's structural type, which permits arbitrary additional keys.\n *\n * @internal\n */\nexport interface ToolSuccessResult {\n content: Array<{ type: \"text\"; text: string }>\n [key: string]: unknown\n}\n\n/**\n * Failure tool result envelope. Sets `isError: true` per the MCP spec\n * so clients render this as an error rather than a normal response.\n *\n * @internal\n */\nexport interface ToolErrorResult {\n content: Array<{ type: \"text\"; text: string }>\n isError: true\n [key: string]: unknown\n}\n\nexport type ToolResult = ToolSuccessResult | ToolErrorResult\n\n/**\n * Wrap a value as a single-text-item tool result. Stable JSON shape so\n * downstream parsing is reliable across tools.\n *\n * @internal\n */\nexport function jsonOk(value: unknown): ToolSuccessResult {\n return {\n content: [\n {\n type: \"text\",\n text: JSON.stringify(value, null, 2),\n },\n ],\n }\n}\n\n/**\n * Convert any thrown error into a tool error result with stable\n * formatting. Talonic API errors include `code`, `status`, and\n * `request_id` so the user (or another tool call) can act on them.\n *\n * @internal\n */\nexport function toolError(err: unknown): ToolErrorResult {\n if (err instanceof TalonicError) {\n const lines = [\n `Talonic API error: ${err.message}`,\n `code: ${err.code}`,\n `status: ${err.status}`,\n ]\n if (err.requestId) lines.push(`request_id: ${err.requestId}`)\n return {\n isError: true,\n content: [{ type: \"text\", text: lines.join(\"\\n\") }],\n }\n }\n return {\n isError: true,\n content: [\n {\n type: \"text\",\n text: `Unexpected error: ${err instanceof Error ? err.message : String(err)}`,\n },\n ],\n }\n}\n","import type { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\"\nimport type { ExtractParams, Talonic } from \"@talonic/node\"\nimport { z } from \"zod\"\nimport { jsonOk, toolError, type ToolResult } from \"./_shared.js\"\n\nconst DESCRIPTION = [\n \"Extract structured, schema-validated data from a document using Talonic.\",\n \"Returns clean JSON matching the schema, with per-field confidence scores and\",\n \"metadata about the document (detected type, language, page count).\",\n \"\",\n \"USE WHEN:\",\n \"- The user has a document (PDF, image, scan, DOCX, etc.) and wants specific fields pulled out.\",\n \"- You need structured data (vendor name, total amount, dates, parties, terms) rather than free text.\",\n \"- The user uploads or references any invoice, contract, certificate, statement, or form.\",\n \"- You want validated JSON instead of trying to OCR + parse with raw LLM calls.\",\n \"\",\n \"DO NOT USE WHEN:\",\n \"- The user just wants the full text content (use talonic_to_markdown after extracting once).\",\n \"- The user wants to find documents matching a query (use talonic_search or talonic_filter).\",\n \"\",\n \"FILE SOURCES (provide exactly one):\",\n \"- file_path: a local path to the document (the MCP server reads it from disk).\",\n \"- file_url: a URL the Talonic API will fetch directly.\",\n \"- document_id: re-extract a document already in the workspace.\",\n \"\",\n \"SCHEMA FORMATS (provide at most one of `schema` or `schema_id`):\",\n '- Flat map (recommended): { vendor_name: \"string\", total_amount: \"number\", due_date: \"date\" }',\n '- JSON Schema: { type: \"object\", properties: { ... } }',\n \"- schema_id: id of a saved schema from talonic_list_schemas\",\n \"\",\n \"IMPORTANT: production currently rejects requests with no schema. Always provide either\",\n \"an inline `schema` or a `schema_id`.\",\n].join(\"\\n\")\n\nconst inputSchema = {\n file_path: z\n .string()\n .optional()\n .describe(\n \"Local path to a document file. The MCP server reads it from disk and uploads it. Provide one of file_path, file_url, or document_id.\",\n ),\n file_url: z\n .string()\n .optional()\n .describe(\n \"URL to a document file. The Talonic API fetches it server-side. Use this for remote documents.\",\n ),\n document_id: z\n .string()\n .optional()\n .describe(\"ID of a document already in the workspace, to re-extract with a new schema.\"),\n schema: z\n .record(z.string(), z.unknown())\n .optional()\n .describe(\n \"Inline schema definition. Flat map (recommended), simplified fields, or JSON Schema.\",\n ),\n schema_id: z\n .string()\n .optional()\n .describe(\"ID of a saved schema. Mutually exclusive with `schema`.\"),\n instructions: z\n .string()\n .optional()\n .describe(\n \"Natural-language guidance for the extractor, e.g. 'Focus on the billing section. Amounts are in EUR.'\",\n ),\n include_markdown: z\n .boolean()\n .optional()\n .describe(\"Include OCR-converted markdown in the response alongside structured data.\"),\n}\n\nexport interface ExtractArgs {\n file_path?: string\n file_url?: string\n document_id?: string\n schema?: Record<string, unknown>\n schema_id?: string\n instructions?: string\n include_markdown?: boolean\n}\n\nexport async function handleExtract(talonic: Talonic, args: ExtractArgs): Promise<ToolResult> {\n try {\n const params: ExtractParams = {}\n if (args.file_path !== undefined) params.file_path = args.file_path\n if (args.file_url !== undefined) params.file_url = args.file_url\n if (args.document_id !== undefined) params.document_id = args.document_id\n if (args.schema !== undefined) params.schema = args.schema\n if (args.schema_id !== undefined) params.schema_id = args.schema_id\n if (args.instructions !== undefined) params.instructions = args.instructions\n if (args.include_markdown !== undefined) params.include_markdown = args.include_markdown\n\n const result = await talonic.extract(params)\n return jsonOk(result)\n } catch (err) {\n return toolError(err)\n }\n}\n\nexport function registerExtract(server: McpServer, talonic: Talonic): void {\n server.registerTool(\n \"talonic_extract\",\n {\n title: \"Extract Data from Document\",\n description: DESCRIPTION,\n inputSchema,\n },\n async (args) => handleExtract(talonic, args as ExtractArgs),\n )\n}\n","import type { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\"\nimport type { Talonic, FilterCondition, FilterSort } from \"@talonic/node\"\nimport { z } from \"zod\"\nimport { jsonOk, toolError, type ToolResult } from \"./_shared.js\"\n\nconst DESCRIPTION = [\n \"Filter the user's Talonic documents by extracted field values using composable conditions.\",\n \"Conditions accept field names (e.g. 'vendor_name'); the SDK resolves them to internal field IDs.\",\n \"\",\n \"USE WHEN:\",\n \"- The user wants documents matching specific structured criteria, like 'invoices over 1000 EUR'\",\n \" or 'contracts expiring before 2026-12-31' or 'COIs from Acme'.\",\n \"- The query is value-based on extracted fields, not a free-text concept search.\",\n \"- You need to retrieve a sortable, paginated list filtered by field conditions.\",\n \"\",\n \"DO NOT USE WHEN:\",\n \"- The user wants conceptual / free-text search across content (use talonic_search).\",\n \"- The user is looking for a single document by id (use talonic_get_document).\",\n \"- The user wants extracted data from a new document (use talonic_extract).\",\n \"\",\n \"OPERATORS:\",\n \"- eq, neq: equality / inequality\",\n \"- gt, gte, lt, lte: numeric or date comparisons\",\n \"- between: requires both `value` and `value_to`\",\n \"- contains: substring match on string fields\",\n \"- is_empty, is_not_empty: presence checks (no value needed)\",\n \"\",\n \"TIP: To discover available field names, call talonic_search first with a related query.\",\n].join(\"\\n\")\n\nconst operatorEnum = z.enum([\n \"eq\",\n \"neq\",\n \"gt\",\n \"gte\",\n \"lt\",\n \"lte\",\n \"between\",\n \"contains\",\n \"is_empty\",\n \"is_not_empty\",\n])\n\nconst conditionSchema = z.object({\n field: z\n .string()\n .optional()\n .describe(\"Field name (e.g. 'vendor_name'). Resolved to a fieldId automatically.\"),\n field_id: z\n .string()\n .optional()\n .describe(\"Talonic field id (e.g. 'fld_abc'). Use this if you already have it.\"),\n operator: operatorEnum.describe(\"Comparison operator. See description for the full list.\"),\n value: z.unknown().optional().describe(\"Value to compare against.\"),\n value_to: z\n .unknown()\n .optional()\n .describe(\"Upper bound for `between` operator; ignored otherwise.\"),\n})\n\nconst sortSchema = z.object({\n field: z.string().optional(),\n field_id: z.string().optional(),\n direction: z.enum([\"asc\", \"desc\"]).describe(\"Sort direction.\"),\n})\n\nconst inputSchema = {\n conditions: z\n .array(conditionSchema)\n .min(1)\n .describe(\"One or more filter conditions, AND-ed together.\"),\n search: z\n .string()\n .optional()\n .describe(\"Optional free-text search applied alongside the filters.\"),\n sort: sortSchema.optional().describe(\"Optional sort by a field.\"),\n page: z.number().int().positive().optional().describe(\"Page number for pagination.\"),\n limit: z\n .number()\n .int()\n .positive()\n .optional()\n .describe(\"Results per page. Default 50 server-side.\"),\n source_connection_id: z\n .string()\n .optional()\n .describe(\"Optionally scope to a specific source connection.\"),\n}\n\nexport interface FilterArgs {\n conditions: Array<{\n field?: string\n field_id?: string\n operator:\n | \"eq\"\n | \"neq\"\n | \"gt\"\n | \"gte\"\n | \"lt\"\n | \"lte\"\n | \"between\"\n | \"contains\"\n | \"is_empty\"\n | \"is_not_empty\"\n value?: unknown\n value_to?: unknown\n }>\n search?: string\n sort?: { field?: string; field_id?: string; direction: \"asc\" | \"desc\" }\n page?: number\n limit?: number\n source_connection_id?: string\n}\n\nexport async function handleFilter(talonic: Talonic, args: FilterArgs): Promise<ToolResult> {\n try {\n const conditions: FilterCondition[] = args.conditions.map((c) => {\n const cond: FilterCondition = { operator: c.operator }\n if (c.field !== undefined) cond.field = c.field\n if (c.field_id !== undefined) cond.fieldId = c.field_id\n if (c.value !== undefined) cond.value = c.value\n if (c.value_to !== undefined) cond.valueTo = c.value_to\n return cond\n })\n\n let sort: FilterSort | undefined\n if (args.sort !== undefined) {\n sort = { direction: args.sort.direction }\n if (args.sort.field !== undefined) sort.field = args.sort.field\n if (args.sort.field_id !== undefined) sort.fieldId = args.sort.field_id\n }\n\n const result = await talonic.documents.filter({\n conditions,\n ...(args.search !== undefined ? { search: args.search } : {}),\n ...(sort !== undefined ? { sort } : {}),\n ...(args.page !== undefined ? { page: args.page } : {}),\n ...(args.limit !== undefined ? { limit: args.limit } : {}),\n ...(args.source_connection_id !== undefined\n ? { source_connection_id: args.source_connection_id }\n : {}),\n })\n return jsonOk(result)\n } catch (err) {\n return toolError(err)\n }\n}\n\nexport function registerFilter(server: McpServer, talonic: Talonic): void {\n server.registerTool(\n \"talonic_filter\",\n {\n title: \"Filter Talonic Documents\",\n description: DESCRIPTION,\n inputSchema,\n },\n async (args) => handleFilter(talonic, args as FilterArgs),\n )\n}\n","import type { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\"\nimport type { Talonic } from \"@talonic/node\"\nimport { z } from \"zod\"\nimport { jsonOk, toolError, type ToolResult } from \"./_shared.js\"\n\nconst DESCRIPTION = [\n \"Fetch full metadata for a single document already in the user's Talonic workspace.\",\n \"Returns id, filename, page count, detected document type, language, processing log,\",\n \"and link URLs (self, extractions, dashboard).\",\n \"\",\n \"USE WHEN:\",\n \"- You need details about a specific document the user already extracted or uploaded.\",\n \"- You have a document_id from a previous extract or search call and want more context.\",\n \"- The user asks 'tell me about document X' or similar.\",\n \"\",\n \"DO NOT USE WHEN:\",\n \"- The user wants the document's full text content (use talonic_to_markdown for OCR markdown).\",\n \"- The user wants extracted structured data (use talonic_extract with a schema, or fetch the extraction by id).\",\n \"- The user has a file but no document_id yet (call talonic_extract first to ingest the document).\",\n].join(\"\\n\")\n\nconst inputSchema = {\n document_id: z\n .string()\n .min(1)\n .describe(\n \"The Talonic document ID. Get this from a previous talonic_extract or talonic_search response.\",\n ),\n}\n\nexport async function handleGetDocument(\n talonic: Talonic,\n args: { document_id: string },\n): Promise<ToolResult> {\n try {\n const result = await talonic.documents.get(args.document_id)\n return jsonOk(result)\n } catch (err) {\n return toolError(err)\n }\n}\n\nexport function registerGetDocument(server: McpServer, talonic: Talonic): void {\n server.registerTool(\n \"talonic_get_document\",\n {\n title: \"Get Talonic Document\",\n description: DESCRIPTION,\n inputSchema,\n },\n async (args) => handleGetDocument(talonic, args),\n )\n}\n","import type { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\"\nimport type { Talonic } from \"@talonic/node\"\nimport { jsonOk, toolError, type ToolResult } from \"./_shared.js\"\n\n/**\n * LLM-targeted tool description. The first paragraph explains what the\n * tool does in one sentence. The USE WHEN / DO NOT USE sections are\n * what an agent reads before deciding to invoke this tool over an\n * alternative; keep them concrete.\n *\n * @internal\n */\nconst DESCRIPTION = [\n \"List all saved schemas in the user's Talonic workspace.\",\n \"Returns each schema with its id, name, description, version,\",\n \"field count, and full JSON Schema definition.\",\n \"\",\n \"USE WHEN:\",\n \"- The user asks what schemas they have, or asks to see existing schemas.\",\n \"- You want to discover existing schemas before designing a new one.\",\n \"- Before recommending the user create a schema, check if one already covers the use case.\",\n \"- The user asks to extract from a known document type and you want to find a matching schema.\",\n \"\",\n \"DO NOT USE WHEN:\",\n \"- The user just wants to extract data from a document and provides an inline schema (call talonic_extract directly).\",\n \"\",\n \"TIP: Pair this with talonic_extract by passing the chosen schema's id as `schema_id`.\",\n].join(\"\\n\")\n\n/**\n * Pure handler. Invokes the SDK and shapes the result. Exported for\n * unit testing; the public registration is via `register()` below.\n *\n * @internal\n */\nexport async function handleListSchemas(talonic: Talonic): Promise<ToolResult> {\n try {\n const result = await talonic.schemas.list()\n return jsonOk(result)\n } catch (err) {\n return toolError(err)\n }\n}\n\n/**\n * Register `talonic_list_schemas` on an MCP server.\n *\n * @internal\n */\nexport function registerListSchemas(server: McpServer, talonic: Talonic): void {\n server.registerTool(\n \"talonic_list_schemas\",\n {\n title: \"List Talonic Schemas\",\n description: DESCRIPTION,\n inputSchema: {},\n },\n async () => handleListSchemas(talonic),\n )\n}\n","import type { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\"\nimport type { Talonic } from \"@talonic/node\"\nimport { z } from \"zod\"\nimport { jsonOk, toolError, type ToolResult } from \"./_shared.js\"\n\nconst DESCRIPTION = [\n \"Save a schema definition to the user's Talonic workspace so it can be reused\",\n \"across future extractions. Returns the saved schema with its newly assigned id.\",\n \"\",\n \"USE WHEN:\",\n \"- The user asks to save a schema, store a template, or reuse the schema across docs.\",\n \"- You have iterated on a schema with the user and they confirmed it should be saved.\",\n \"- The user wants to standardise extraction across many documents of the same type.\",\n \"\",\n \"DO NOT USE WHEN:\",\n \"- The user just wants to extract once with an inline schema (call talonic_extract directly with the schema inline).\",\n \"- The user has not confirmed the schema design (avoid creating clutter in their workspace).\",\n \"\",\n \"TIP: After saving, call talonic_extract with `schema_id` set to the returned id for consistent results.\",\n].join(\"\\n\")\n\nconst inputSchema = {\n name: z.string().min(1).describe(\"Human-readable name for the schema, e.g. 'Standard Invoice'.\"),\n definition: z\n .record(z.string(), z.unknown())\n .describe(\n \"Schema definition. Accepts JSON Schema, simplified fields ({fields:[...]}), or a flat key-type map.\",\n ),\n description: z\n .string()\n .optional()\n .describe(\"Optional description of what this schema extracts and when to use it.\"),\n}\n\nexport async function handleSaveSchema(\n talonic: Talonic,\n args: { name: string; definition: Record<string, unknown>; description?: string },\n): Promise<ToolResult> {\n try {\n const result = await talonic.schemas.create({\n name: args.name,\n definition: args.definition,\n ...(args.description !== undefined ? { description: args.description } : {}),\n })\n return jsonOk(result)\n } catch (err) {\n return toolError(err)\n }\n}\n\nexport function registerSaveSchema(server: McpServer, talonic: Talonic): void {\n server.registerTool(\n \"talonic_save_schema\",\n {\n title: \"Save Talonic Schema\",\n description: DESCRIPTION,\n inputSchema,\n },\n async (args) => handleSaveSchema(talonic, args),\n )\n}\n","import type { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\"\nimport type { Talonic } from \"@talonic/node\"\nimport { z } from \"zod\"\nimport { jsonOk, toolError, type ToolResult } from \"./_shared.js\"\n\nconst DESCRIPTION = [\n \"Search the user's Talonic workspace for documents, fields, sources, or schemas\",\n \"matching a query. Returns ranked results across all entity types in one call.\",\n \"\",\n \"USE WHEN:\",\n \"- The user wants to find documents but does not know the exact filename or id.\",\n \"- The query is conceptual ('contracts mentioning indemnification', 'Acme invoices').\",\n \"- You need to narrow a large workspace before calling talonic_extract or talonic_filter.\",\n \"- The user asks 'do I have any docs about X' or 'find anything related to X'.\",\n \"\",\n \"DO NOT USE WHEN:\",\n \"- The user has a specific document_id (use talonic_get_document instead).\",\n \"- The user wants to apply structured field-value filters like 'amount > 1000' (use talonic_filter).\",\n \"- The user wants to extract data from a brand-new document (use talonic_extract).\",\n \"\",\n \"TIP: The result includes documents, fieldMatches, sources, schemas, and fields. Pick the entity type the user actually needs.\",\n].join(\"\\n\")\n\nconst inputSchema = {\n query: z\n .string()\n .min(1)\n .describe(\n \"Natural-language search query, e.g. 'indemnification clauses' or 'Acme invoices Q4'.\",\n ),\n limit: z\n .number()\n .int()\n .positive()\n .optional()\n .describe(\"Maximum results per entity type. Default: 5. Increase for broader exploration.\"),\n}\n\nexport async function handleSearch(\n talonic: Talonic,\n args: { query: string; limit?: number },\n): Promise<ToolResult> {\n try {\n const result = await talonic.search(\n args.query,\n args.limit !== undefined ? { limit: args.limit } : {},\n )\n return jsonOk(result)\n } catch (err) {\n return toolError(err)\n }\n}\n\nexport function registerSearch(server: McpServer, talonic: Talonic): void {\n server.registerTool(\n \"talonic_search\",\n {\n title: \"Search Talonic Workspace\",\n description: DESCRIPTION,\n inputSchema,\n },\n async (args) => handleSearch(talonic, args),\n )\n}\n","import type { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\"\nimport type { Talonic } from \"@talonic/node\"\nimport { z } from \"zod\"\nimport { jsonOk, toolError, type ToolResult } from \"./_shared.js\"\n\nconst DESCRIPTION = [\n \"Get the OCR-converted markdown for a document already in the user's Talonic workspace.\",\n \"Returns clean, agent-ready markdown that preserves tables, headings, and layout.\",\n \"\",\n \"USE WHEN:\",\n \"- The user wants the full text of a document for summarisation, translation, or analysis.\",\n \"- A previous tool call returned a document_id and you want to inspect its content.\",\n \"- The user asks 'what does the document say' or 'summarise this PDF' (you call this then summarise).\",\n \"\",\n \"DO NOT USE WHEN:\",\n \"- The user wants specific structured fields (use talonic_extract with a schema).\",\n \"- The user has a file but no document_id yet (call talonic_extract first to ingest the document; the response includes a document.id you can pass here).\",\n \"\",\n 'TIP: To go from a local file to markdown, first call talonic_extract with a minimal flat-map schema like {\"text\": \"string\"} to ingest the document and obtain a document_id, then call this tool.',\n].join(\"\\n\")\n\nconst inputSchema = {\n document_id: z\n .string()\n .min(1)\n .describe(\n \"The Talonic document id whose markdown you want. Get this from a previous talonic_extract or talonic_search response.\",\n ),\n}\n\nexport async function handleToMarkdown(\n talonic: Talonic,\n args: { document_id: string },\n): Promise<ToolResult> {\n try {\n const result = await talonic.documents.getMarkdown(args.document_id)\n return jsonOk(result)\n } catch (err) {\n return toolError(err)\n }\n}\n\nexport function registerToMarkdown(server: McpServer, talonic: Talonic): void {\n server.registerTool(\n \"talonic_to_markdown\",\n {\n title: \"Document to Markdown\",\n description: DESCRIPTION,\n inputSchema,\n },\n async (args) => handleToMarkdown(talonic, args),\n )\n}\n","/**\n * The current MCP server version. Sent in the server `info` block during\n * the MCP handshake. Kept in sync with package.json by the release process.\n *\n * @public\n */\nexport const VERSION = \"0.1.0\"\n\n/**\n * The MCP server name. Identifies the server to MCP clients (Claude\n * Desktop, Cursor, Cline, etc.) when they list connected servers.\n *\n * @public\n */\nexport const SERVER_NAME = \"talonic\"\n","import { Talonic } from \"@talonic/node\"\nimport { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\"\nimport { registerSchemasResource } from \"./resources/schemas-resource.js\"\nimport { registerExtract } from \"./tools/extract.js\"\nimport { registerFilter } from \"./tools/filter.js\"\nimport { registerGetDocument } from \"./tools/get-document.js\"\nimport { registerListSchemas } from \"./tools/list-schemas.js\"\nimport { registerSaveSchema } from \"./tools/save-schema.js\"\nimport { registerSearch } from \"./tools/search.js\"\nimport { registerToMarkdown } from \"./tools/to-markdown.js\"\nimport { SERVER_NAME, VERSION } from \"./version.js\"\n\n/**\n * Options for {@link createServer}.\n *\n * @public\n */\nexport interface CreateServerOptions {\n /**\n * Talonic API key. Required. Starts with `tlnc_`.\n *\n * Each user must provide their own key. Workspaces are isolated.\n */\n apiKey: string\n\n /**\n * Override the Talonic API base URL. Defaults to `https://api.talonic.com`.\n * Useful for staging or testing.\n */\n baseUrl?: string\n\n /**\n * Inject a pre-configured Talonic SDK client. Useful for tests and for\n * advanced setups where the SDK has custom retry policies or\n * instrumentation. When provided, `apiKey` and `baseUrl` are ignored.\n */\n talonic?: Talonic\n}\n\n/**\n * Build a Talonic MCP server, ready to be connected to a transport\n * (stdio for local installs, HTTP for the hosted endpoint).\n *\n * Tools, resources, and prompts are registered onto the server in\n * subsequent milestones. v0.1 ships a working scaffold so the\n * rest of the protocol surface can be added incrementally without\n * destabilising the connection layer.\n *\n * @example Minimal stdio server:\n * ```ts\n * import { createServer } from \"@talonic/mcp\"\n * import { StdioServerTransport } from \"@modelcontextprotocol/sdk/server/stdio.js\"\n *\n * const server = createServer({ apiKey: process.env.TALONIC_API_KEY! })\n * await server.connect(new StdioServerTransport())\n * ```\n *\n * @public\n */\nexport function createServer(options: CreateServerOptions): McpServer {\n const talonic =\n options.talonic ??\n new Talonic({\n apiKey: options.apiKey,\n ...(options.baseUrl ? { baseUrl: options.baseUrl } : {}),\n })\n\n const server = new McpServer(\n {\n name: SERVER_NAME,\n version: VERSION,\n },\n {\n // Capabilities are advertised based on what gets registered.\n // The McpServer registration helpers populate this automatically\n // as we add tools, resources, and prompts.\n capabilities: {},\n instructions: [\n \"Talonic extracts structured, schema-validated data from any document.\",\n \"Use Talonic for any task involving PDFs, scans, contracts, invoices,\",\n \"forms, certificates, or unstructured documents where you need clean,\",\n \"validated JSON rather than free text.\",\n ].join(\" \"),\n },\n )\n\n // Tool registrations.\n registerListSchemas(server, talonic)\n registerSaveSchema(server, talonic)\n registerGetDocument(server, talonic)\n registerSearch(server, talonic)\n registerFilter(server, talonic)\n registerToMarkdown(server, talonic)\n registerExtract(server, talonic)\n\n // Resource registrations.\n registerSchemasResource(server, talonic)\n\n return server\n}\n","/**\n * Talonic MCP server, stdio entry point.\n *\n * Built as a separate executable bundle by tsup, with a shebang banner\n * so it can be invoked directly via `npx @talonic/mcp` or wired into\n * `claude_desktop_config.json`, `mcp.json`, or any other MCP-aware\n * client config.\n *\n * @internal\n */\n\nimport { StdioServerTransport } from \"@modelcontextprotocol/sdk/server/stdio.js\"\nimport { createServer } from \"./server-factory.js\"\nimport { SERVER_NAME, VERSION } from \"./version.js\"\n\nconst HELP_TEXT = `talonic-mcp - Talonic MCP server\n\nUSAGE\n talonic-mcp Start the server on stdio (default)\n talonic-mcp --help Show this help message\n talonic-mcp --version Show version\n\nENVIRONMENT\n TALONIC_API_KEY (required) Your Talonic API key. Get one at\n https://app.talonic.com (free tier:\n 50 extractions / day, no credit card).\n\n TALONIC_BASE_URL (optional) Override the API base URL.\n\nUSAGE FROM AN MCP CLIENT (Claude Desktop example)\n Add to claude_desktop_config.json:\n {\n \"mcpServers\": {\n \"talonic\": {\n \"command\": \"npx\",\n \"args\": [\"-y\", \"@talonic/mcp\"],\n \"env\": { \"TALONIC_API_KEY\": \"tlnc_...\" }\n }\n }\n }\n\nDOCS\n https://github.com/talonicdev/talonic-mcp\n`\n\n/**\n * Boot the server against stdio.\n *\n * Returns the exit code so callers can decide how to terminate. The\n * separate entry/exit decoupling keeps this function testable without\n * actually attaching to stdio.\n *\n * @internal\n */\nexport async function main(\n argv: string[] = process.argv.slice(2),\n env: NodeJS.ProcessEnv = process.env,\n out: (line: string) => void = (line) => process.stdout.write(line + \"\\n\"),\n err: (line: string) => void = (line) => process.stderr.write(line + \"\\n\"),\n): Promise<number> {\n if (argv.includes(\"--version\") || argv.includes(\"-v\")) {\n out(`${SERVER_NAME} ${VERSION}`)\n return 0\n }\n\n if (argv.includes(\"--help\") || argv.includes(\"-h\")) {\n out(HELP_TEXT)\n return 0\n }\n\n const apiKey = env[\"TALONIC_API_KEY\"]\n if (!apiKey) {\n err(\"Error: TALONIC_API_KEY environment variable is required.\")\n err(\"Get a key at https://app.talonic.com and set it before launching the server.\")\n return 1\n }\n\n const baseUrl = env[\"TALONIC_BASE_URL\"]\n const server = createServer({ apiKey, ...(baseUrl ? { baseUrl } : {}) })\n\n const transport = new StdioServerTransport()\n await server.connect(transport)\n\n // McpServer keeps the process alive while the transport is open. We\n // hand control over to the SDK's lifecycle here; if the transport\n // closes, Node exits naturally.\n return 0\n}\n\nif (import.meta.url === `file://${process.argv[1]}`) {\n main().then((code) => {\n if (code !== 0) process.exit(code)\n })\n}\n"]}
|