@abhishek944/pi-classifier 0.4.0 → 0.5.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 +33 -0
- package/README.md +269 -31
- package/dist/auth.d.ts +4 -2
- package/dist/auth.js +18 -2
- package/dist/box-tool.d.ts +2 -24
- package/dist/box-tool.js +5 -35
- package/dist/choices.js +35 -1
- package/dist/classify.d.ts +3 -0
- package/dist/classify.js +3 -3
- package/dist/config.d.ts +140 -3
- package/dist/config.js +401 -20
- package/dist/cost.js +42 -4
- package/dist/durable.d.ts +34 -0
- package/dist/durable.js +237 -0
- package/dist/extension.d.ts +2 -275
- package/dist/extension.js +49 -211
- package/dist/extract.d.ts +11 -0
- package/dist/extract.js +191 -0
- package/dist/http.d.ts +6 -1
- package/dist/http.js +64 -28
- package/dist/index.d.ts +3 -1
- package/dist/index.js +1 -0
- package/dist/inputs.d.ts +16 -1
- package/dist/inputs.js +421 -14
- package/dist/mcp-cli.d.ts +2 -0
- package/dist/mcp-cli.js +39 -0
- package/dist/mcp-server.d.ts +28 -0
- package/dist/mcp-server.js +121 -0
- package/dist/mcp-tool-definitions.d.ts +19 -0
- package/dist/mcp-tool-definitions.js +64 -0
- package/dist/providers.d.ts +1 -0
- package/dist/providers.js +1144 -53
- package/dist/tool-contracts.d.ts +785 -0
- package/dist/tool-contracts.js +530 -0
- package/dist/tree.js +1 -1
- package/dist/types.d.ts +284 -8
- package/dist/validation.js +122 -10
- package/package.json +32 -5
- package/src/auth.ts +26 -2
- package/src/box-tool.ts +12 -47
- package/src/choices.ts +42 -1
- package/src/classify.ts +6 -4
- package/src/config.ts +450 -23
- package/src/cost.ts +42 -4
- package/src/durable.ts +298 -0
- package/src/extension.ts +75 -288
- package/src/extract.ts +228 -0
- package/src/http.ts +63 -32
- package/src/index.ts +15 -0
- package/src/inputs.ts +454 -13
- package/src/mcp-cli.ts +44 -0
- package/src/mcp-server.ts +179 -0
- package/src/mcp-tool-definitions.ts +104 -0
- package/src/providers.ts +1323 -56
- package/src/tool-contracts.ts +785 -0
- package/src/tree.ts +1 -1
- package/src/types.ts +231 -5
- package/src/validation.ts +114 -9
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// Independent MCP server for the classifier package. Tool behavior stays in
|
|
2
|
+
// the owning package's core functions; this module only lists tools, validates
|
|
3
|
+
// arguments at the MCP boundary, forwards cancellation, and maps results to
|
|
4
|
+
// MCP content.
|
|
5
|
+
import { createRequire } from 'node:module';
|
|
6
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
7
|
+
import { CallToolRequestSchema, ErrorCode, ListToolsRequestSchema, McpError, } from '@modelcontextprotocol/sdk/types.js';
|
|
8
|
+
import { renderClassifierBox } from './box.js';
|
|
9
|
+
import { classify, compareClassifiers } from './classify.js';
|
|
10
|
+
import { loadClassifierSettings } from './config.js';
|
|
11
|
+
import { extract } from './extract.js';
|
|
12
|
+
import { CLASSIFIER_BOX_TOOL, CLASSIFY_COMPARE_TOOL, CLASSIFY_EXTRACT_TOOL, CLASSIFY_TOOL, CLASSIFY_TREE_TOOL, } from './tool-contracts.js';
|
|
13
|
+
import { classifyTree } from './tree.js';
|
|
14
|
+
import { userError } from './types.js';
|
|
15
|
+
import { CLASSIFIER_MCP_TOOL_DEFINITIONS, validateMcpToolArguments, } from './mcp-tool-definitions.js';
|
|
16
|
+
const require = createRequire(import.meta.url);
|
|
17
|
+
const manifest = require('../package.json');
|
|
18
|
+
const packageVersion = typeof manifest === 'object' && manifest !== null && 'version' in manifest && typeof manifest.version === 'string'
|
|
19
|
+
? manifest.version
|
|
20
|
+
: '0.0.0';
|
|
21
|
+
export const classifierMcpServerInfo = {
|
|
22
|
+
name: 'pi-classifier',
|
|
23
|
+
version: packageVersion,
|
|
24
|
+
};
|
|
25
|
+
function errorResult(text) {
|
|
26
|
+
return { content: [{ type: 'text', text }], isError: true };
|
|
27
|
+
}
|
|
28
|
+
function jsonResult(value) {
|
|
29
|
+
return {
|
|
30
|
+
content: [{ type: 'text', text: JSON.stringify(value, null, 2) }],
|
|
31
|
+
structuredContent: { result: value },
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** Create an MCP Server. The CLI connects it to stdio; embedders own any other transport. */
|
|
35
|
+
export function createClassifierMcpServer(options = {}) {
|
|
36
|
+
const cwd = options.cwd ?? process.cwd();
|
|
37
|
+
const settings = options.settings ?? loadClassifierSettings(cwd, options.projectTrusted ?? false);
|
|
38
|
+
const credentialSource = options.credentialSource ?? 'explicit';
|
|
39
|
+
const enabled = options.enabledTools;
|
|
40
|
+
const tools = enabled === undefined
|
|
41
|
+
? CLASSIFIER_MCP_TOOL_DEFINITIONS
|
|
42
|
+
: CLASSIFIER_MCP_TOOL_DEFINITIONS.filter((tool) => enabled.includes(tool.name));
|
|
43
|
+
const server = new Server(options.info ?? classifierMcpServerInfo, {
|
|
44
|
+
capabilities: { tools: {} },
|
|
45
|
+
instructions: 'Independent pi-classifier MCP server. Provider routes and credentials stay on the server; relative paths resolve against the server process cwd. Classification and comparison calls may incur charges and send supplied state to the configured provider.',
|
|
46
|
+
});
|
|
47
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
48
|
+
tools: tools.map((tool) => ({
|
|
49
|
+
name: tool.name,
|
|
50
|
+
description: tool.description,
|
|
51
|
+
inputSchema: tool.inputSchema,
|
|
52
|
+
annotations: tool.annotations,
|
|
53
|
+
})),
|
|
54
|
+
}));
|
|
55
|
+
server.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
|
|
56
|
+
const name = request.params.name;
|
|
57
|
+
const tool = tools.find((candidate) => candidate.name === name);
|
|
58
|
+
if (tool === undefined)
|
|
59
|
+
throw new McpError(ErrorCode.InvalidParams, `Unknown tool: ${name}`);
|
|
60
|
+
const args = request.params.arguments ?? {};
|
|
61
|
+
const problems = validateMcpToolArguments(tool, args);
|
|
62
|
+
if (problems.length > 0)
|
|
63
|
+
return errorResult(`Invalid arguments for ${name}:\n- ${problems.join('\n- ')}`);
|
|
64
|
+
const signal = extra.signal;
|
|
65
|
+
const requestOptions = {
|
|
66
|
+
settings,
|
|
67
|
+
cwd,
|
|
68
|
+
credentialSource,
|
|
69
|
+
...(signal ? { signal } : {}),
|
|
70
|
+
};
|
|
71
|
+
try {
|
|
72
|
+
switch (tool.name) {
|
|
73
|
+
case CLASSIFY_TOOL:
|
|
74
|
+
return jsonResult(await classify(args, requestOptions));
|
|
75
|
+
case CLASSIFY_COMPARE_TOOL:
|
|
76
|
+
return jsonResult(await compareClassifiers(args, requestOptions));
|
|
77
|
+
case CLASSIFY_TREE_TOOL:
|
|
78
|
+
return jsonResult(await classifyTree(args, requestOptions));
|
|
79
|
+
case CLASSIFY_EXTRACT_TOOL:
|
|
80
|
+
return jsonResult(await extract(args, requestOptions));
|
|
81
|
+
case CLASSIFIER_BOX_TOOL: {
|
|
82
|
+
const result = await renderClassifierBox(args, {
|
|
83
|
+
cwd,
|
|
84
|
+
...(signal ? { signal } : {}),
|
|
85
|
+
});
|
|
86
|
+
const byteLength = Buffer.from(result.data, 'base64').byteLength;
|
|
87
|
+
const details = {
|
|
88
|
+
mode: result.mode,
|
|
89
|
+
mimeType: result.mimeType,
|
|
90
|
+
width: result.width,
|
|
91
|
+
height: result.height,
|
|
92
|
+
pixelBox: result.pixelBox,
|
|
93
|
+
byteLength,
|
|
94
|
+
};
|
|
95
|
+
return {
|
|
96
|
+
content: [
|
|
97
|
+
{
|
|
98
|
+
type: 'text',
|
|
99
|
+
text: `Rendered ${result.mode} box as a ${result.width}×${result.height} PNG ` +
|
|
100
|
+
`(${byteLength} bytes). Source pixel box: left=${result.pixelBox.left}, ` +
|
|
101
|
+
`top=${result.pixelBox.top}, width=${result.pixelBox.width}, ` +
|
|
102
|
+
`height=${result.pixelBox.height}. A box is a localization hint, not proof that the object exists.`,
|
|
103
|
+
},
|
|
104
|
+
{ type: 'image', data: result.data, mimeType: result.mimeType },
|
|
105
|
+
],
|
|
106
|
+
structuredContent: { result: details },
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
default: {
|
|
110
|
+
const exhaustive = tool.name;
|
|
111
|
+
return errorResult(`Unhandled tool: ${exhaustive}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
return errorResult(`${tool.name} failed: ${userError(error)}`);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
return server;
|
|
120
|
+
}
|
|
121
|
+
export { CLASSIFIER_MCP_TOOL_DEFINITIONS, validateMcpToolArguments, } from './mcp-tool-definitions.js';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { TSchema } from 'typebox';
|
|
2
|
+
import { CLASSIFIER_BOX_TOOL, CLASSIFY_COMPARE_TOOL, CLASSIFY_EXTRACT_TOOL, CLASSIFY_TOOL, CLASSIFY_TREE_TOOL } from './tool-contracts.js';
|
|
3
|
+
export type ClassifierMcpToolName = typeof CLASSIFY_TOOL | typeof CLASSIFY_COMPARE_TOOL | typeof CLASSIFY_TREE_TOOL | typeof CLASSIFY_EXTRACT_TOOL | typeof CLASSIFIER_BOX_TOOL;
|
|
4
|
+
export interface ClassifierMcpToolAnnotations {
|
|
5
|
+
readOnlyHint: boolean;
|
|
6
|
+
destructiveHint: boolean;
|
|
7
|
+
idempotentHint?: boolean;
|
|
8
|
+
openWorldHint: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface ClassifierMcpToolDefinition {
|
|
11
|
+
name: ClassifierMcpToolName;
|
|
12
|
+
description: string;
|
|
13
|
+
inputSchema: TSchema;
|
|
14
|
+
annotations: ClassifierMcpToolAnnotations;
|
|
15
|
+
}
|
|
16
|
+
/** Excludes Pi-only computer use; all portable package tools are exposed. */
|
|
17
|
+
export declare const CLASSIFIER_MCP_TOOL_DEFINITIONS: readonly ClassifierMcpToolDefinition[];
|
|
18
|
+
/** Return human-readable validation problems; an empty array means the arguments are valid. */
|
|
19
|
+
export declare function validateMcpToolArguments(tool: ClassifierMcpToolDefinition, args: unknown): string[];
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// MCP-only catalog for the standalone stdio server. The argument schemas come
|
|
2
|
+
// from the package-owned tool contracts shared with the Pi extension and the
|
|
3
|
+
// Durable factory; this module only adapts them to MCP metadata and validation.
|
|
4
|
+
import { Value } from 'typebox/value';
|
|
5
|
+
import { CLASSIFIER_BOX_TOOL, CLASSIFIER_BOX_TOOL_DESCRIPTION, CLASSIFY_COMPARE_TOOL, CLASSIFY_COMPARE_TOOL_DESCRIPTION, CLASSIFY_EXTRACT_TOOL, CLASSIFY_EXTRACT_TOOL_DESCRIPTION, CLASSIFY_TOOL, CLASSIFY_TOOL_DESCRIPTION, CLASSIFY_TREE_TOOL, CLASSIFY_TREE_TOOL_DESCRIPTION, boxParameters, classifyExtractParameters, classifyParameters, classifyTreeParameters, } from './tool-contracts.js';
|
|
6
|
+
const providerAnnotations = {
|
|
7
|
+
readOnlyHint: true,
|
|
8
|
+
destructiveHint: false,
|
|
9
|
+
openWorldHint: true,
|
|
10
|
+
};
|
|
11
|
+
const localAnnotations = {
|
|
12
|
+
readOnlyHint: true,
|
|
13
|
+
destructiveHint: false,
|
|
14
|
+
openWorldHint: false,
|
|
15
|
+
};
|
|
16
|
+
/** Excludes Pi-only computer use; all portable package tools are exposed. */
|
|
17
|
+
export const CLASSIFIER_MCP_TOOL_DEFINITIONS = [
|
|
18
|
+
{
|
|
19
|
+
name: CLASSIFY_TOOL,
|
|
20
|
+
description: CLASSIFY_TOOL_DESCRIPTION,
|
|
21
|
+
inputSchema: classifyParameters,
|
|
22
|
+
annotations: providerAnnotations,
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
name: CLASSIFY_COMPARE_TOOL,
|
|
26
|
+
description: CLASSIFY_COMPARE_TOOL_DESCRIPTION,
|
|
27
|
+
inputSchema: classifyParameters,
|
|
28
|
+
annotations: providerAnnotations,
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: CLASSIFY_TREE_TOOL,
|
|
32
|
+
description: CLASSIFY_TREE_TOOL_DESCRIPTION,
|
|
33
|
+
inputSchema: classifyTreeParameters,
|
|
34
|
+
annotations: providerAnnotations,
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: CLASSIFY_EXTRACT_TOOL,
|
|
38
|
+
description: CLASSIFY_EXTRACT_TOOL_DESCRIPTION,
|
|
39
|
+
inputSchema: classifyExtractParameters,
|
|
40
|
+
annotations: providerAnnotations,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: CLASSIFIER_BOX_TOOL,
|
|
44
|
+
description: CLASSIFIER_BOX_TOOL_DESCRIPTION,
|
|
45
|
+
inputSchema: boxParameters,
|
|
46
|
+
annotations: localAnnotations,
|
|
47
|
+
},
|
|
48
|
+
];
|
|
49
|
+
/** Return human-readable validation problems; an empty array means the arguments are valid. */
|
|
50
|
+
export function validateMcpToolArguments(tool, args) {
|
|
51
|
+
if (Value.Check(tool.inputSchema, args))
|
|
52
|
+
return [];
|
|
53
|
+
const problems = new Set();
|
|
54
|
+
for (const error of Value.Errors(tool.inputSchema, args)) {
|
|
55
|
+
if (error.keyword === 'boolean')
|
|
56
|
+
continue;
|
|
57
|
+
const path = error.instancePath || 'arguments';
|
|
58
|
+
const unknown = error.keyword === 'additionalProperties'
|
|
59
|
+
? error.params.additionalProperties
|
|
60
|
+
: undefined;
|
|
61
|
+
problems.add(unknown?.length ? `${path}: unknown properties ${unknown.join(', ')}` : `${path}: ${error.message}`);
|
|
62
|
+
}
|
|
63
|
+
return problems.size > 0 ? [...problems] : ['arguments do not match the input schema'];
|
|
64
|
+
}
|
package/dist/providers.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { type ClassifierResult, type PreparedInput, type ResolvedModel } from "./types.js";
|
|
2
2
|
export declare function classifyWithProvider(route: ResolvedModel, input: PreparedInput, apiKey: string, signal?: AbortSignal): Promise<ClassifierResult>;
|
|
3
|
+
export declare function unwrapCloudflare(label: string, body: unknown): Record<string, unknown>;
|