@wiro-ai/wiro-agent-connectors 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.
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ // The host side of the package: what a program needs to run connector modules
3
+ // itself, and what the stdio CLI is built from.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.serveStdio = exports.createConnectorServer = exports.VENDOR_PATTERN = exports.loadConnector = exports.listVendors = exports.DEFAULT_VENDORS_DIR = exports.assertConnectorModule = exports.isPublicAddress = exports.createGuardedFetch = exports.ConnectorFetchError = exports.envNameFor = exports.credentialsFromEnv = exports.accountsFromEnv = exports.ACCOUNTS_ENV = void 0;
6
+ var env_js_1 = require("./env.js");
7
+ Object.defineProperty(exports, "ACCOUNTS_ENV", { enumerable: true, get: function () { return env_js_1.ACCOUNTS_ENV; } });
8
+ Object.defineProperty(exports, "accountsFromEnv", { enumerable: true, get: function () { return env_js_1.accountsFromEnv; } });
9
+ Object.defineProperty(exports, "credentialsFromEnv", { enumerable: true, get: function () { return env_js_1.credentialsFromEnv; } });
10
+ Object.defineProperty(exports, "envNameFor", { enumerable: true, get: function () { return env_js_1.envNameFor; } });
11
+ var guarded_fetch_js_1 = require("./guarded-fetch.js");
12
+ Object.defineProperty(exports, "ConnectorFetchError", { enumerable: true, get: function () { return guarded_fetch_js_1.ConnectorFetchError; } });
13
+ Object.defineProperty(exports, "createGuardedFetch", { enumerable: true, get: function () { return guarded_fetch_js_1.createGuardedFetch; } });
14
+ var ip_js_1 = require("./ip.js");
15
+ Object.defineProperty(exports, "isPublicAddress", { enumerable: true, get: function () { return ip_js_1.isPublicAddress; } });
16
+ var load_js_1 = require("./load.js");
17
+ Object.defineProperty(exports, "assertConnectorModule", { enumerable: true, get: function () { return load_js_1.assertConnectorModule; } });
18
+ Object.defineProperty(exports, "DEFAULT_VENDORS_DIR", { enumerable: true, get: function () { return load_js_1.DEFAULT_VENDORS_DIR; } });
19
+ Object.defineProperty(exports, "listVendors", { enumerable: true, get: function () { return load_js_1.listVendors; } });
20
+ Object.defineProperty(exports, "loadConnector", { enumerable: true, get: function () { return load_js_1.loadConnector; } });
21
+ Object.defineProperty(exports, "VENDOR_PATTERN", { enumerable: true, get: function () { return load_js_1.VENDOR_PATTERN; } });
22
+ var server_js_1 = require("./server.js");
23
+ Object.defineProperty(exports, "createConnectorServer", { enumerable: true, get: function () { return server_js_1.createConnectorServer; } });
24
+ Object.defineProperty(exports, "serveStdio", { enumerable: true, get: function () { return server_js_1.serveStdio; } });
@@ -0,0 +1 @@
1
+ export declare function isPublicAddress(input: string): boolean;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.isPublicAddress = isPublicAddress;
7
+ const node_net_1 = __importDefault(require("node:net"));
8
+ // Which addresses a media download may connect to: public unicast only. No
9
+ // loopback, private, link-local, carrier-grade NAT, documentation, multicast or
10
+ // reserved ranges, in IPv4 or IPv6. IPv4-mapped IPv6 addresses are checked
11
+ // against the IPv4 rules (net.BlockList does that itself).
12
+ const blocked = new node_net_1.default.BlockList();
13
+ for (const [address, prefix] of [
14
+ ['0.0.0.0', 8],
15
+ ['10.0.0.0', 8],
16
+ ['100.64.0.0', 10],
17
+ ['127.0.0.0', 8],
18
+ ['169.254.0.0', 16],
19
+ ['172.16.0.0', 12],
20
+ ['192.0.0.0', 24],
21
+ ['192.0.2.0', 24],
22
+ ['192.88.99.0', 24],
23
+ ['192.168.0.0', 16],
24
+ ['198.18.0.0', 15],
25
+ ['198.51.100.0', 24],
26
+ ['203.0.113.0', 24],
27
+ ['224.0.0.0', 4],
28
+ ['240.0.0.0', 4],
29
+ ]) {
30
+ blocked.addSubnet(address, prefix, 'ipv4');
31
+ }
32
+ for (const [address, prefix] of [
33
+ ['::', 96], // unspecified, loopback and the deprecated IPv4-compatible range
34
+ ['64:ff9b:1::', 48], // local-use NAT64
35
+ ['100::', 64], // discard
36
+ ['2001::', 23], // IETF protocol assignments, Teredo included
37
+ ['2001:db8::', 32], // documentation
38
+ ['2002::', 16], // 6to4
39
+ ['fc00::', 7], // unique local
40
+ ['fe80::', 10], // link-local
41
+ ['fec0::', 10], // site-local
42
+ ['ff00::', 8], // multicast
43
+ ]) {
44
+ blocked.addSubnet(address, prefix, 'ipv6');
45
+ }
46
+ /** The 8 groups of an IPv6 address, or undefined. */
47
+ function ipv6Groups(address) {
48
+ let text = address;
49
+ const dotted = /(\d+\.\d+\.\d+\.\d+)$/.exec(text);
50
+ if (dotted) {
51
+ const parts = dotted[1].split('.').map(Number);
52
+ text = `${text.slice(0, -dotted[1].length)}${((parts[0] << 8) | parts[1]).toString(16)}:${((parts[2] << 8) | parts[3]).toString(16)}`;
53
+ }
54
+ const [head = '', tail] = text.split('::');
55
+ const left = head ? head.split(':') : [];
56
+ const right = tail !== undefined && tail ? tail.split(':') : [];
57
+ const missing = 8 - left.length - right.length;
58
+ if (tail === undefined ? missing !== 0 : missing < 1)
59
+ return undefined;
60
+ const groups = [...left, ...Array(tail === undefined ? 0 : missing).fill('0'), ...right].map((group) => Number.parseInt(group, 16));
61
+ return groups.length === 8 && groups.every((group) => Number.isInteger(group) && group >= 0 && group <= 0xffff) ? groups : undefined;
62
+ }
63
+ function isPublicAddress(input) {
64
+ const address = input.replace(/^\[|\]$/g, '').replace(/%.*$/, '');
65
+ const family = node_net_1.default.isIP(address);
66
+ if (family === 4)
67
+ return !blocked.check(address, 'ipv4');
68
+ if (family !== 6)
69
+ return false;
70
+ if (blocked.check(address, 'ipv6'))
71
+ return false;
72
+ const groups = ipv6Groups(address.toLowerCase());
73
+ if (!groups)
74
+ return false;
75
+ // NAT64 (64:ff9b::/96) carries an IPv4 address: judge that one.
76
+ if (groups[0] === 0x64 && groups[1] === 0xff9b && groups.slice(2, 6).every((group) => group === 0)) {
77
+ const [high = 0, low = 0] = groups.slice(6);
78
+ return isPublicAddress(`${high >> 8}.${high & 0xff}.${low >> 8}.${low & 0xff}`);
79
+ }
80
+ return true;
81
+ }
@@ -0,0 +1,12 @@
1
+ import type { ConnectorModule } from '../types.js';
2
+ export declare const VENDOR_PATTERN: RegExp;
3
+ /** dist/vendors, next to this package's dist/host. */
4
+ export declare const DEFAULT_VENDORS_DIR: string;
5
+ export declare function listVendors(dir?: string): string[];
6
+ /** Throws unless the value looks like a connector module for `vendor`. */
7
+ export declare function assertConnectorModule(value: unknown, vendor: string): asserts value is ConnectorModule;
8
+ /**
9
+ * Loads dist/vendors/<vendor>.js. The name must match the vendor pattern, and
10
+ * the file must really be inside the directory (no "..", no symlink out).
11
+ */
12
+ export declare function loadConnector(vendor: string, dir?: string): ConnectorModule;
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DEFAULT_VENDORS_DIR = exports.VENDOR_PATTERN = void 0;
7
+ exports.listVendors = listVendors;
8
+ exports.assertConnectorModule = assertConnectorModule;
9
+ exports.loadConnector = loadConnector;
10
+ const node_fs_1 = __importDefault(require("node:fs"));
11
+ const node_path_1 = __importDefault(require("node:path"));
12
+ exports.VENDOR_PATTERN = /^[a-z][a-z0-9-]{1,29}$/;
13
+ /** dist/vendors, next to this package's dist/host. */
14
+ exports.DEFAULT_VENDORS_DIR = node_path_1.default.resolve(__dirname, '..', 'vendors');
15
+ function listVendors(dir = exports.DEFAULT_VENDORS_DIR) {
16
+ let names;
17
+ try {
18
+ names = node_fs_1.default.readdirSync(dir);
19
+ }
20
+ catch {
21
+ return [];
22
+ }
23
+ return names
24
+ .filter((name) => name.endsWith('.js'))
25
+ .map((name) => name.slice(0, -3))
26
+ .filter((name) => exports.VENDOR_PATTERN.test(name))
27
+ .sort();
28
+ }
29
+ /** Throws unless the value looks like a connector module for `vendor`. */
30
+ function assertConnectorModule(value, vendor) {
31
+ const module = value;
32
+ const problems = [];
33
+ if (!module || typeof module !== 'object')
34
+ problems.push('it exports no object');
35
+ else {
36
+ if (module.vendor !== vendor)
37
+ problems.push(`vendor is ${JSON.stringify(module.vendor)}`);
38
+ if (!Array.isArray(module.origins) || !module.origins.length)
39
+ problems.push('origins is missing');
40
+ if (!Array.isArray(module.credentials))
41
+ problems.push('credentials is missing');
42
+ if (!Array.isArray(module.tools))
43
+ problems.push('tools is missing');
44
+ if (typeof module.call !== 'function')
45
+ problems.push('call is missing');
46
+ }
47
+ if (problems.length)
48
+ throw new Error(`${vendor}.js is not a connector module: ${problems.join(', ')}.`);
49
+ }
50
+ /**
51
+ * Loads dist/vendors/<vendor>.js. The name must match the vendor pattern, and
52
+ * the file must really be inside the directory (no "..", no symlink out).
53
+ */
54
+ function loadConnector(vendor, dir = exports.DEFAULT_VENDORS_DIR) {
55
+ if (!exports.VENDOR_PATTERN.test(vendor))
56
+ throw new Error(`"${vendor}" is not a connector name.`);
57
+ const root = node_fs_1.default.realpathSync(dir);
58
+ let file;
59
+ try {
60
+ file = node_fs_1.default.realpathSync(node_path_1.default.join(root, `${vendor}.js`));
61
+ }
62
+ catch {
63
+ throw new Error(`There is no connector named "${vendor}".`);
64
+ }
65
+ if (node_path_1.default.dirname(file) !== root)
66
+ throw new Error(`There is no connector named "${vendor}".`);
67
+ const module = require(file);
68
+ assertConnectorModule(module, vendor);
69
+ return module;
70
+ }
@@ -0,0 +1,21 @@
1
+ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
2
+ import type { ConnectorAccounts, ConnectorFetch, ConnectorModule } from '../types.js';
3
+ export interface ConnectorServerOptions {
4
+ credentials: Record<string, string>;
5
+ accounts: ConnectorAccounts;
6
+ /** Defaults to a guarded fetch locked to the module's origins. */
7
+ fetch?: ConnectorFetch;
8
+ /** Where log lines go (default: stderr). Never stdout: that is the protocol. */
9
+ log?: (text: string) => void;
10
+ /** Answer for a credential the host knows is missing, e.g. naming the environment variable. */
11
+ missingCredentials?: string;
12
+ version?: string;
13
+ }
14
+ /**
15
+ * An MCP server for one connector module. The server is named after the
16
+ * vendor, lists the module's tools as they are, and runs every call through
17
+ * module.call with a fresh context.
18
+ */
19
+ export declare function createConnectorServer(module: ConnectorModule, options: ConnectorServerOptions): Server;
20
+ /** Serves one connector module over stdio until stdin closes. */
21
+ export declare function serveStdio(module: ConnectorModule, options: ConnectorServerOptions): Promise<Server>;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createConnectorServer = createConnectorServer;
4
+ exports.serveStdio = serveStdio;
5
+ const index_js_1 = require("@modelcontextprotocol/sdk/server/index.js");
6
+ const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
7
+ const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
8
+ const guarded_fetch_js_1 = require("./guarded-fetch.js");
9
+ function errorResult(message) {
10
+ return { content: [{ type: 'text', text: JSON.stringify({ error: message }) }], isError: true };
11
+ }
12
+ /**
13
+ * An MCP server for one connector module. The server is named after the
14
+ * vendor, lists the module's tools as they are, and runs every call through
15
+ * module.call with a fresh context.
16
+ */
17
+ function createConnectorServer(module, options) {
18
+ const log = options.log ?? ((text) => process.stderr.write(`${text}\n`));
19
+ const fetch = options.fetch ?? (0, guarded_fetch_js_1.createGuardedFetch)({ origins: module.origins });
20
+ const server = new index_js_1.Server({ name: module.vendor, version: options.version ?? '0.0.0' }, { capabilities: { tools: {} } });
21
+ server.setRequestHandler(types_js_1.ListToolsRequestSchema, async () => ({ tools: module.tools.map((tool) => ({ ...tool })) }));
22
+ server.setRequestHandler(types_js_1.CallToolRequestSchema, async (request, extra) => {
23
+ const name = request.params.name;
24
+ const started = Date.now();
25
+ let result;
26
+ if (options.missingCredentials) {
27
+ result = errorResult(options.missingCredentials);
28
+ }
29
+ else {
30
+ try {
31
+ result = await module.call(name, request.params.arguments ?? {}, {
32
+ credentials: { ...options.credentials },
33
+ accounts: options.accounts,
34
+ fetch,
35
+ signal: extra.signal,
36
+ log,
37
+ });
38
+ }
39
+ catch {
40
+ // A module returns its errors as results; a throw is a bug, and its message stays out of the reply.
41
+ result = errorResult(`${name} failed unexpectedly.`);
42
+ }
43
+ }
44
+ log(`${module.vendor} ${name} ${result.isError ? 'error' : 'ok'} ${Date.now() - started}ms`);
45
+ return { ...result };
46
+ });
47
+ return server;
48
+ }
49
+ /** Serves one connector module over stdio until stdin closes. */
50
+ async function serveStdio(module, options) {
51
+ const server = createConnectorServer(module, options);
52
+ await server.connect(new stdio_js_1.StdioServerTransport());
53
+ return server;
54
+ }
@@ -0,0 +1,82 @@
1
+ /** The JSON Schema subset tool inputs use. Every schema property is optional. */
2
+ export interface JsonSchema {
3
+ type?: 'object' | 'string' | 'integer' | 'number' | 'boolean' | 'array';
4
+ description?: string;
5
+ properties?: Record<string, JsonSchema>;
6
+ required?: string[];
7
+ additionalProperties?: boolean;
8
+ items?: JsonSchema;
9
+ minItems?: number;
10
+ maxItems?: number;
11
+ uniqueItems?: boolean;
12
+ enum?: ReadonlyArray<string | number | boolean>;
13
+ minLength?: number;
14
+ maxLength?: number;
15
+ pattern?: string;
16
+ format?: 'uri';
17
+ minimum?: number;
18
+ maximum?: number;
19
+ default?: unknown;
20
+ }
21
+ /** MCP tool annotations. Modules set all four, derived from the tool's kind. */
22
+ export interface ToolAnnotations {
23
+ readOnlyHint: boolean;
24
+ destructiveHint: boolean;
25
+ idempotentHint: boolean;
26
+ openWorldHint: boolean;
27
+ }
28
+ /** One entry of tools/list. */
29
+ export interface ToolDescriptor {
30
+ name: string;
31
+ title: string;
32
+ description: string;
33
+ inputSchema: JsonSchema;
34
+ annotations: ToolAnnotations;
35
+ }
36
+ export interface TextContent {
37
+ type: 'text';
38
+ text: string;
39
+ }
40
+ /** An MCP CallToolResult. Modules return a single JSON text block. */
41
+ export interface CallToolResult {
42
+ content: TextContent[];
43
+ isError?: boolean;
44
+ }
45
+ /** What a module may pass to ctx.fetch. */
46
+ export interface ConnectorRequestInit {
47
+ method?: string;
48
+ headers?: Record<string, string>;
49
+ body?: string | Uint8Array;
50
+ signal?: AbortSignal;
51
+ }
52
+ /**
53
+ * The host's guarded fetch. A request to one of the module's origins is an API
54
+ * call; any other URL is a media download (GET only, public https addresses
55
+ * only). It resolves to a standard Response or rejects; a rejection whose
56
+ * `code` is "URL_REFUSED" means nothing was sent.
57
+ */
58
+ export type ConnectorFetch = (url: string, init?: ConnectorRequestInit) => Promise<Response>;
59
+ /**
60
+ * Account selections, e.g. { "pages": ["123"] }. null means the host applies
61
+ * no account lock (the stdio CLI without CONNECTOR_ACCOUNTS).
62
+ */
63
+ export type ConnectorAccounts = Record<string, string[]> | null;
64
+ /** Passed to every call. Nothing in it outlives the call inside the module, except cached sessions. */
65
+ export interface ToolContext {
66
+ credentials: Record<string, string>;
67
+ accounts: ConnectorAccounts;
68
+ fetch: ConnectorFetch;
69
+ signal?: AbortSignal;
70
+ log(text: string): void;
71
+ }
72
+ /** What dist/vendors/<vendor>.js exports. */
73
+ export interface ConnectorModule {
74
+ /** ^[a-z][a-z0-9-]{1,29}$, equal to the file name. */
75
+ vendor: string;
76
+ /** The only API origins the module calls, e.g. ["https://dev.to"]. */
77
+ origins: string[];
78
+ /** The ctx.credentials names the module reads, e.g. ["api_key"]. */
79
+ credentials: string[];
80
+ tools: ToolDescriptor[];
81
+ call(toolName: string, args: unknown, ctx: ToolContext): Promise<CallToolResult>;
82
+ }
package/dist/types.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ // The connector module contract. A host (the stdio CLI in this package, or any
3
+ // other MCP host) loads dist/vendors/<vendor>.js and talks to it only through
4
+ // these shapes. Modules are dependency-free CommonJS and use web-standard
5
+ // globals only; every network request goes through ctx.fetch.
6
+ Object.defineProperty(exports, "__esModule", { value: true });