-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathindex.ts
More file actions
373 lines (336 loc) · 14.2 KB
/
Copy pathindex.ts
File metadata and controls
373 lines (336 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/env node
// @utcp/code-mode-cli — call UTCP tools via code mode from any shell.
//
// Output contract: every command prints JSON to stdout. Most print a single
// object; `login` prints NDJSON (one object per line). Errors print
// {"error": "..."} and exit non-zero. Human/diagnostic noise goes to stderr.
import { promises as fs } from 'fs';
import path from 'path';
import util from 'util';
// Keep stdout reserved for machine-readable JSON: route UTCP's diagnostic
// console output (log/info/warn) to stderr. Tool-chain console output is
// captured separately by callToolChain and returned in `logs`.
console.log = (...args: any[]) => process.stderr.write(util.format(...args) + '\n');
console.info = (...args: any[]) => process.stderr.write(util.format(...args) + '\n');
console.warn = (...args: any[]) => process.stderr.write(util.format(...args) + '\n');
// Protocol plugins register themselves on import (side effects).
import '@utcp/http';
import '@utcp/text';
import '@utcp/mcp';
import '@utcp/cli';
import '@utcp/dotenv-loader';
import '@utcp/file';
import { CodeModeUtcpClient } from '@utcp/code-mode';
import { createClient, resolveConfigPath } from './src/config.js';
import { utcpNameToTsInterfaceName, findToolByName } from './src/names.js';
import {
httpDeviceLogin,
httpAuthCodeStart,
mcpLoginStart,
loginFinishWithCode,
DEFAULT_REDIRECT,
} from './src/oauth.js';
import { validateConfig, validateManualFiles } from './src/validate.js';
// ---------------------------------------------------------------------------
// arg parsing
// ---------------------------------------------------------------------------
interface Args {
command: string;
positionals: string[];
config?: string;
limit?: number;
code?: string;
file?: string;
timeout?: number;
paste: boolean;
authCode?: string;
offline: boolean;
manualMode: boolean;
}
function parseArgs(argv: string[]): Args {
const a: Args = { command: argv[0] || 'help', positionals: [], paste: false, offline: false, manualMode: false };
for (let i = 1; i < argv.length; i++) {
const t = argv[i];
switch (t) {
case '--config': a.config = argv[++i]; break;
case '--limit': a.limit = Number(argv[++i]); break;
case '-c': case '--code-string': a.code = argv[++i]; break;
case '--file': a.file = argv[++i]; break;
case '--timeout': a.timeout = Number(argv[++i]); break;
case '--paste': a.paste = true; break;
case '--code': a.authCode = argv[++i]; break;
case '--offline': a.offline = true; break;
case '--manual': a.manualMode = true; break;
case '-h': case '--help': a.command = a.command === 'help' ? 'help' : a.command; a.positionals.push('--help'); break;
default: a.positionals.push(t);
}
}
return a;
}
function out(obj: unknown): void {
process.stdout.write(JSON.stringify(obj) + '\n');
}
function fail(message: string): never {
process.stdout.write(JSON.stringify({ error: message }) + '\n');
process.exit(1);
}
async function readStdin(): Promise<string> {
if (process.stdin.isTTY) return '';
const chunks: Buffer[] = [];
for await (const c of process.stdin) chunks.push(c as Buffer);
return Buffer.concat(chunks).toString('utf-8');
}
// ---------------------------------------------------------------------------
// guide text
// ---------------------------------------------------------------------------
const SELF_CONFIG_GUIDE = `
## Self-configuration (no environment variables needed)
You configure tools by writing a \`.utcp_config.json\` file in your working
directory, then running \`utcp\` against it. Minimal example:
{
"load_variables_from": [
{ "variable_loader_type": "dotenv", "env_file_path": ".env" }
],
"manual_call_templates": [
{
"name": "openlibrary",
"call_template_type": "http",
"http_method": "GET",
"url": "https://openlibrary.org/static/openapi.json",
"content_type": "application/json"
}
]
}
### Workflow
1. Write \`.utcp_config.json\` with the manuals (APIs / MCP servers / CLIs) you need.
2. \`utcp validate\` to confirm the config + manuals are well-formed and that
each manual registers (use \`--offline\` to skip the network and only check shape).
3. \`utcp search "<task>"\` to discover tools + their TypeScript interfaces.
4. Write a tool-chain in TypeScript and run it:
utcp run <<'EOF'
const r = await openlibrary.read_search_json({ q: "tolkien" });
return r.docs.slice(0, 3).map(b => b.title);
EOF
5. Tools are called as \`manual.tool({ args })\`. Top-level \`await\` and \`return\` work.
### You can load an API from any description — not just a UTCP call template
- **OpenAPI / Swagger spec (URL):** use an \`http\` manual whose \`url\` points at the
spec; UTCP auto-converts it to tools. (The openlibrary example above is exactly this.)
- **OpenAPI spec you have as a file, OR a natural-language API description:** YOU
write a UTCP manual JSON file (a \`{ "utcp_version": "...", "tools": [...] }\`
document — each tool has name/description/inputs/outputs/tool_call_template),
then load it with a \`file\` manual:
{ "name": "myapi", "call_template_type": "file", "file_path": "./myapi_manual.json" }
Validate the manual you wrote first: \`utcp validate --manual ./myapi_manual.json\`
- When unsure of the manual schema, run \`utcp validate --manual <file>\` and fix
the reported errors until it passes; then add the \`file\` manual to your config and
\`utcp validate\` the whole config.
### Authentication
- API key / basic / OAuth2 client-credentials: put values (or \`\${VAR}\` refs) in the
manual's \`auth\` block; provide \`\${VAR}\` values via the \`.env\` dotenv loader.
- Interactive sign-in (user opens a page): declare an \`oauth2_user\` auth block:
"auth": { "auth_type": "oauth2_user", "access_token": "\${MY_TOKEN}",
"grant_type": "device_code",
"device_authorization_endpoint": "...", "token_endpoint": "...",
"client_id": "..." , "scope": "..." }
For MCP servers, only \`{ "auth_type": "oauth2_user", "access_token": "\${MY_TOKEN}" }\`
is needed — endpoints are auto-discovered. Then run:
utcp login <manual>
and follow the printed instructions (show the user the URL). \`login\` writes the
token into \`.env\`; UTCP injects it on every call afterwards.
`.trim();
function helpText(): string {
return [
'utcp — call UTCP tools via code mode from any shell. No MCP required.',
'',
'Commands:',
' prompt Print the full agent guide (read this first).',
' search <query> [--limit N] Find tools; returns TypeScript interfaces.',
' list List all registered tool names.',
' info <tool...> Print TypeScript interfaces for tools.',
' keys <tool> List required variables (e.g. tokens) for a tool.',
' run [code] [-c <code>] [--file <p>] [--timeout <ms>]',
' Execute a TS tool-chain (also reads stdin).',
' login <manual> [--paste] [--code <code|url>]',
' Interactive OAuth sign-in; writes token to .env.',
' validate [--offline] Validate .utcp_config.json + its manuals.',
' validate --manual <file...> Validate standalone UTCP manual file(s).',
'',
'Global: --config <path> Use a specific .utcp_config.json',
'',
'Run `utcp prompt` for the full self-configuration + login guide.',
].join('\n');
}
// ---------------------------------------------------------------------------
// login helpers
// ---------------------------------------------------------------------------
/** Re-derives the namespaced dotenv key UTCP looks up for `${var}` in a manual. */
function namespacedKey(manualName: string, varName: string): string {
return manualName.replace(/_/g, '!').replace(/!/g, '__') + '_' + varName;
}
function varNameFromTemplate(accessToken: string): string | null {
const m = /\$\{?([a-zA-Z0-9_]+)\}?/.exec(accessToken || '');
return m ? m[1]! : null;
}
async function readRawConfig(configPath: string): Promise<any> {
try {
return JSON.parse(await fs.readFile(configPath, 'utf-8'));
} catch {
return {};
}
}
function dotenvPath(rawConfig: any): string {
const loaders: any[] = rawConfig.load_variables_from || [];
const dotenv = loaders.find((l) => l && l.variable_loader_type === 'dotenv');
const rel = dotenv?.env_file_path || '.env';
// The dotenv loader resolves env_file_path relative to process.cwd().
return path.resolve(process.cwd(), rel);
}
async function handleLogin(args: Args): Promise<void> {
const target = args.positionals[0];
if (!target) fail('Usage: utcp login <manual> [--paste] [--code <code|url>]');
const manual = target.includes('.') ? target.split('.')[0]! : target;
// Step 2 of any paste flow: just exchange the code.
if (args.authCode) {
await loginFinishWithCode(manual, args.authCode);
return;
}
const configPath = await resolveConfigPath(args.config);
const raw = await readRawConfig(configPath);
const entry = (raw.manual_call_templates || []).find((m: any) => m.name === manual);
if (!entry) fail(`Manual '${manual}' not found in ${configPath}.`);
const auth = entry.auth || {};
if (auth.auth_type !== 'oauth2_user') {
fail(`Manual '${manual}' does not declare an 'oauth2_user' auth block; nothing to log in to.`);
}
const varName = varNameFromTemplate(auth.access_token);
if (!varName) fail(`Manual '${manual}' oauth2_user.access_token must reference a variable like "\${MY_TOKEN}".`);
const envKey = namespacedKey(manual, varName!);
const envPath = dotenvPath(raw);
const redirect = auth.redirect_uri || DEFAULT_REDIRECT;
if (entry.call_template_type === 'mcp') {
const servers = entry.config?.mcpServers || {};
const first: any = Object.values(servers)[0];
const serverUrl = first?.url;
if (!serverUrl) fail(`MCP manual '${manual}' has no http server url to discover OAuth from.`);
await mcpLoginStart({ manual, serverUrl, scope: auth.scope, redirect, envPath, envKey });
return;
}
// HTTP manual: choose flow.
const grant = args.paste
? 'authorization_code'
: auth.grant_type || (auth.device_authorization_endpoint ? 'device_code' : 'authorization_code');
if (grant === 'device_code') {
if (!auth.device_authorization_endpoint || !auth.token_endpoint || !auth.client_id) {
fail(`device_code flow needs device_authorization_endpoint, token_endpoint and client_id in the oauth2_user block.`);
}
await httpDeviceLogin({
manual,
deviceEndpoint: auth.device_authorization_endpoint,
tokenEndpoint: auth.token_endpoint,
clientId: auth.client_id,
scope: auth.scope,
envPath,
envKey,
});
} else {
if (!auth.authorization_endpoint || !auth.token_endpoint || !auth.client_id) {
fail(`authorization_code flow needs authorization_endpoint, token_endpoint and client_id in the oauth2_user block.`);
}
await httpAuthCodeStart({
manual,
authEndpoint: auth.authorization_endpoint,
tokenEndpoint: auth.token_endpoint,
clientId: auth.client_id,
scope: auth.scope,
redirect,
envPath,
envKey,
});
}
}
// ---------------------------------------------------------------------------
// main
// ---------------------------------------------------------------------------
async function main(): Promise<void> {
const args = parseArgs(process.argv.slice(2));
if (['help', '--help', '-h'].includes(args.command) || args.positionals.includes('--help')) {
process.stdout.write(helpText() + '\n');
return;
}
if (args.command === 'prompt') {
process.stdout.write(
CodeModeUtcpClient.AGENT_PROMPT_TEMPLATE + '\n\n' + SELF_CONFIG_GUIDE + '\n',
);
return;
}
if (args.command === 'login') {
await handleLogin(args);
return;
}
if (args.command === 'validate') {
if (args.manualMode) {
if (args.positionals.length === 0) fail('Usage: utcp validate --manual <file...>');
out(await validateManualFiles(args.positionals));
} else {
out(await validateConfig(args.config, args.offline));
}
return;
}
// All remaining commands need a client.
const { client } = await createClient(args.config);
switch (args.command) {
case 'search': {
const query = args.positionals.join(' ');
if (!query) fail('Usage: utcp search <query> [--limit N]');
const tools = await client.searchTools(query, args.limit ?? 10);
out({
tools: tools.map((t) => ({
name: utcpNameToTsInterfaceName(t.name),
description: t.description,
typescript_interface: client.toolToTypeScriptInterface(t),
})),
});
break;
}
case 'list': {
const tools = await client.config.tool_repository.getTools();
out({ tools: tools.map((t) => utcpNameToTsInterfaceName(t.name)) });
break;
}
case 'info': {
if (args.positionals.length === 0) fail('Usage: utcp info <tool...>');
const parts: string[] = [];
for (const name of args.positionals) {
const found = await findToolByName(client, name);
parts.push(found ? client.toolToTypeScriptInterface(found.tool) : `// Tool '${name}' not found`);
}
out({ interfaces: parts.join('\n\n') });
break;
}
case 'keys': {
const name = args.positionals[0];
if (!name) fail('Usage: utcp keys <tool>');
const found = await findToolByName(client, name);
if (!found) fail(`Tool '${name}' not found`);
const vars = await client.getRequiredVariablesForRegisteredTool(found!.utcpName);
out({ tool: name, required_variables: vars });
break;
}
case 'run': {
let code = args.code ?? '';
if (!code && args.file) code = await fs.readFile(args.file, 'utf-8');
if (!code && args.positionals.length) code = args.positionals.join(' ');
if (!code) code = await readStdin();
if (!code.trim()) fail('No code provided. Pass via -c, --file, a positional, or stdin.');
const { result, logs } = await client.callToolChain(code, args.timeout ?? 30000);
out({ success: true, result, logs });
break;
}
default:
fail(`Unknown command '${args.command}'. Run \`utcp help\`.`);
}
await client.close().catch(() => {});
}
main().catch((e) => {
fail(e instanceof Error ? e.message : String(e));
});