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
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.1.0-alpha.27"
".": "0.1.0-alpha.28"
}
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# Changelog

## 0.1.0-alpha.28 (2026-02-27)

Full Changelog: [v0.1.0-alpha.27...v0.1.0-alpha.28](https://github.com/Scan-Documents/node-sdk/compare/v0.1.0-alpha.27...v0.1.0-alpha.28)

### Features

* **mcp:** add an option to disable code tool ([da647e2](https://github.com/Scan-Documents/node-sdk/commit/da647e2d55b35d55460133d6787eef4f28d896ef))


### Chores

* **internal:** fix MCP Dockerfiles so they can be built without buildkit ([e1a34c1](https://github.com/Scan-Documents/node-sdk/commit/e1a34c1c73ba4bf2cd8aed8e96e857c194fedc9a))
* **internal:** fix MCP Dockerfiles so they can be built without buildkit ([4b55b1f](https://github.com/Scan-Documents/node-sdk/commit/4b55b1fae74f0ea3106258ccf85863a9172582c2))
* **internal:** make MCP code execution location configurable via a flag ([1133800](https://github.com/Scan-Documents/node-sdk/commit/1133800e1e21c15a3beb23b14dc3e917b6248146))
* **internal:** move stringifyQuery implementation to internal function ([3c528b4](https://github.com/Scan-Documents/node-sdk/commit/3c528b41d05fc52d1259d88192f6a9db5ed9717c))
* **internal:** upgrade @modelcontextprotocol/sdk and hono ([cfd3f73](https://github.com/Scan-Documents/node-sdk/commit/cfd3f730692dcebcab5597407ef7927e057c39da))

## 0.1.0-alpha.27 (2026-02-24)

Full Changelog: [v0.1.0-alpha.26...v0.1.0-alpha.27](https://github.com/Scan-Documents/node-sdk/compare/v0.1.0-alpha.26...v0.1.0-alpha.27)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scan-documents",
"version": "0.1.0-alpha.27",
"version": "0.1.0-alpha.28",
"description": "The official TypeScript library for the Scan Documents API",
"author": "Scan Documents <support@scan-documents.com>",
"types": "dist/index.d.ts",
Expand Down
9 changes: 7 additions & 2 deletions packages/mcp-server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ COPY . .
RUN yarn install --frozen-lockfile && \
yarn build

# Production stage
FROM node:24-alpine
FROM denoland/deno:alpine-2.7.1

# Install node and npm
RUN apk add --no-cache nodejs npm

ENV LD_LIBRARY_PATH=/usr/lib:/usr/local/lib

# Add non-root user
RUN addgroup -g 1001 -S nodejs && adduser -S nodejs -u 1001
Expand All @@ -57,6 +61,7 @@ COPY --from=builder /build/dist ./node_modules/scan-documents

# Change ownership to nodejs user
RUN chown -R nodejs:nodejs /app
RUN chown -R nodejs:nodejs /deno-dir

# Switch to non-root user
USER nodejs
Expand Down
2 changes: 1 addition & 1 deletion packages/mcp-server/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"dxt_version": "0.2",
"name": "scan-documents-mcp",
"version": "0.1.0-alpha.27",
"version": "0.1.0-alpha.28",
"description": "The official MCP Server for the Scan Documents API",
"author": {
"name": "Scan Documents",
Expand Down
4 changes: 2 additions & 2 deletions packages/mcp-server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scan-documents-mcp",
"version": "0.1.0-alpha.27",
"version": "0.1.0-alpha.28",
"description": "The official MCP Server for the Scan Documents API",
"author": "Scan Documents <support@scan-documents.com>",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -32,7 +32,7 @@
"dependencies": {
"scan-documents": "file:../../dist/",
"@cloudflare/cabidela": "^0.2.4",
"@modelcontextprotocol/sdk": "^1.25.2",
"@modelcontextprotocol/sdk": "^1.26.0",
"@valtown/deno-http-worker": "^0.0.21",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
Expand Down
3 changes: 3 additions & 0 deletions packages/mcp-server/src/code-tool-paths.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

export const workerPath = require.resolve('./code-tool-worker.mjs');
1 change: 1 addition & 0 deletions packages/mcp-server/src/code-tool-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type WorkerInput = {
client_opts: ClientOptions;
intent?: string | undefined;
};

export type WorkerOutput = {
is_error: boolean;
result: unknown | null;
Expand Down
288 changes: 288 additions & 0 deletions packages/mcp-server/src/code-tool-worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

import path from 'node:path';
import util from 'node:util';
import Fuse from 'fuse.js';
import ts from 'typescript';
import { WorkerOutput } from './code-tool-types';
import { ScanDocuments, ClientOptions } from 'scan-documents';

function getRunFunctionSource(code: string): {
type: 'declaration' | 'expression';
client: string | undefined;
code: string;
} | null {
const sourceFile = ts.createSourceFile('code.ts', code, ts.ScriptTarget.Latest, true);
const printer = ts.createPrinter();

for (const statement of sourceFile.statements) {
// Check for top-level function declarations
if (ts.isFunctionDeclaration(statement)) {
if (statement.name?.text === 'run') {
return {
type: 'declaration',
client: statement.parameters[0]?.name.getText(),
code: printer.printNode(ts.EmitHint.Unspecified, statement.body!, sourceFile),
};
}
}

// Check for variable declarations: const run = () => {} or const run = function() {}
if (ts.isVariableStatement(statement)) {
for (const declaration of statement.declarationList.declarations) {
if (
ts.isIdentifier(declaration.name) &&
declaration.name.text === 'run' &&
// Check if it's initialized with a function
declaration.initializer &&
(ts.isFunctionExpression(declaration.initializer) || ts.isArrowFunction(declaration.initializer))
) {
return {
type: 'expression',
client: declaration.initializer.parameters[0]?.name.getText(),
code: printer.printNode(ts.EmitHint.Unspecified, declaration.initializer, sourceFile),
};
}
}
}
}

return null;
}

function getTSDiagnostics(code: string): string[] {
const functionSource = getRunFunctionSource(code)!;
const codeWithImport = [
'import { ScanDocuments } from "scan-documents";',
functionSource.type === 'declaration' ?
`async function run(${functionSource.client}: ScanDocuments)`
: `const run: (${functionSource.client}: ScanDocuments) => Promise<unknown> =`,
functionSource.code,
].join('\n');
const sourcePath = path.resolve('code.ts');
const ast = ts.createSourceFile(sourcePath, codeWithImport, ts.ScriptTarget.Latest, true);
const options = ts.getDefaultCompilerOptions();
options.target = ts.ScriptTarget.Latest;
options.module = ts.ModuleKind.NodeNext;
options.moduleResolution = ts.ModuleResolutionKind.NodeNext;
const host = ts.createCompilerHost(options, true);
const newHost: typeof host = {
...host,
getSourceFile: (...args) => {
if (path.resolve(args[0]) === sourcePath) {
return ast;
}
return host.getSourceFile(...args);
},
readFile: (...args) => {
if (path.resolve(args[0]) === sourcePath) {
return codeWithImport;
}
return host.readFile(...args);
},
fileExists: (...args) => {
if (path.resolve(args[0]) === sourcePath) {
return true;
}
return host.fileExists(...args);
},
};
const program = ts.createProgram({
options,
rootNames: [sourcePath],
host: newHost,
});
const diagnostics = ts.getPreEmitDiagnostics(program, ast);
return diagnostics.map((d) => {
const message = ts.flattenDiagnosticMessageText(d.messageText, '\n');
if (!d.file || !d.start) return `- ${message}`;
const { line: lineNumber } = ts.getLineAndCharacterOfPosition(d.file, d.start);
const line = codeWithImport.split('\n').at(lineNumber)?.trim();
return line ? `- ${message}\n ${line}` : `- ${message}`;
});
}

const fuse = new Fuse(
[
'client.files.delete',
'client.files.download',
'client.files.list',
'client.files.retrieve',
'client.files.upload',
'client.tasks.list',
'client.tasks.retrieve',
'client.events.list',
'client.imageOperations.applyEffect',
'client.imageOperations.convert',
'client.imageOperations.detectDocuments',
'client.imageOperations.extractText',
'client.imageOperations.scan',
'client.imageOperations.warp',
'client.pdfOperations.extractPages',
'client.pdfOperations.merge',
'client.pdfOperations.render',
'client.pdfOperations.split',
],
{ threshold: 1, shouldSort: true },
);

function getMethodSuggestions(fullyQualifiedMethodName: string): string[] {
return fuse
.search(fullyQualifiedMethodName)
.map(({ item }) => item)
.slice(0, 5);
}

const proxyToObj = new WeakMap<any, any>();
const objToProxy = new WeakMap<any, any>();

type ClientProxyConfig = {
path: string[];
isBelievedBad?: boolean;
};

function makeSdkProxy<T extends object>(obj: T, { path, isBelievedBad = false }: ClientProxyConfig): T {
let proxy: T = objToProxy.get(obj);

if (!proxy) {
proxy = new Proxy(obj, {
get(target, prop, receiver) {
const propPath = [...path, String(prop)];
const value = Reflect.get(target, prop, receiver);

if (isBelievedBad || (!(prop in target) && value === undefined)) {
// If we're accessing a path that doesn't exist, it will probably eventually error.
// Let's proxy it and mark it bad so that we can control the error message.
// We proxy an empty class so that an invocation or construction attempt is possible.
return makeSdkProxy(class {}, { path: propPath, isBelievedBad: true });
}

if (value !== null && (typeof value === 'object' || typeof value === 'function')) {
return makeSdkProxy(value, { path: propPath, isBelievedBad });
}

return value;
},

apply(target, thisArg, args) {
if (isBelievedBad || typeof target !== 'function') {
const fullyQualifiedMethodName = path.join('.');
const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
throw new Error(
`${fullyQualifiedMethodName} is not a function. Did you mean: ${suggestions.join(', ')}`,
);
}

return Reflect.apply(target, proxyToObj.get(thisArg) ?? thisArg, args);
},

construct(target, args, newTarget) {
if (isBelievedBad || typeof target !== 'function') {
const fullyQualifiedMethodName = path.join('.');
const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
throw new Error(
`${fullyQualifiedMethodName} is not a constructor. Did you mean: ${suggestions.join(', ')}`,
);
}

return Reflect.construct(target, args, newTarget);
},
});

objToProxy.set(obj, proxy);
proxyToObj.set(proxy, obj);
}

return proxy;
}

function parseError(code: string, error: unknown): string | undefined {
if (!(error instanceof Error)) return;
const message = error.name ? `${error.name}: ${error.message}` : error.message;
try {
// Deno uses V8; the first "<anonymous>:LINE:COLUMN" is the top of stack.
const lineNumber = error.stack?.match(/<anonymous>:([0-9]+):[0-9]+/)?.[1];
// -1 for the zero-based indexing
const line =
lineNumber &&
code
.split('\n')
.at(parseInt(lineNumber, 10) - 1)
?.trim();
return line ? `${message}\n at line ${lineNumber}\n ${line}` : message;
} catch {
return message;
}
}

const fetch = async (req: Request): Promise<Response> => {
const { opts, code } = (await req.json()) as { opts: ClientOptions; code: string };

const runFunctionSource = code ? getRunFunctionSource(code) : null;
if (!runFunctionSource) {
const message =
code ?
'The code is missing a top-level `run` function.'
: 'The code argument is missing. Provide one containing a top-level `run` function.';
return Response.json(
{
is_error: true,
result: `${message} Write code within this template:\n\n\`\`\`\nasync function run(client) {\n // Fill this out\n}\n\`\`\``,
log_lines: [],
err_lines: [],
} satisfies WorkerOutput,
{ status: 400, statusText: 'Code execution error' },
);
}

const diagnostics = getTSDiagnostics(code);
if (diagnostics.length > 0) {
return Response.json(
{
is_error: true,
result: `The code contains TypeScript diagnostics:\n${diagnostics.join('\n')}`,
log_lines: [],
err_lines: [],
} satisfies WorkerOutput,
{ status: 400, statusText: 'Code execution error' },
);
}

const client = new ScanDocuments({
...opts,
});

const log_lines: string[] = [];
const err_lines: string[] = [];
const console = {
log: (...args: unknown[]) => {
log_lines.push(util.format(...args));
},
error: (...args: unknown[]) => {
err_lines.push(util.format(...args));
},
};
try {
let run_ = async (client: any) => {};
eval(`${code}\nrun_ = run;`);
const result = await run_(makeSdkProxy(client, { path: ['client'] }));
return Response.json({
is_error: false,
result,
log_lines,
err_lines,
} satisfies WorkerOutput);
} catch (e) {
return Response.json(
{
is_error: true,
result: parseError(code, e),
log_lines,
err_lines,
} satisfies WorkerOutput,
{ status: 400, statusText: 'Code execution error' },
);
}
};

export default { fetch };
Loading