@snowluma/mcp gains execution alongside the read-only catalog: when SNOWLUMA_MCP_ENDPOINT is set it exposes query_action (read-only actions, readOnlyHint) and, in write mode, invoke_action (any known action, destructiveHint), proxied to a running OneBot instance over HTTP. Without an endpoint it degrades to docs-only. SNOWLUMA_MCP_MODE (docs|read|write) gates tool visibility AND is re-checked in execute(), so an unlisted tool cannot be called; unknown/legacy actions are non-invokable. Read vs write is classified precisely at the source: defineAction/preset gain readOnly (default false = write, the safe default), flowed through describe() into the generated catalog snapshot. 58 actions are marked read-only by what their run() actually does -- name-traps like get_ai_record (publishes to the group), request_decrypt_key (returns a DB key), and download_file (arbitrary URL + disk write) stay writes. The HTTP call is a ~20-line fetch bridge rather than @snowluma/sdk: the SDK's published dist is bundler-only ESM (extensionless imports) that native node cannot load, and the bin stays a plain tsc build. onebot 332 tests pass (readOnly is metadata-only, no behavior change); mcp gains 21 tests for mode gating / read-write routing / envelope passthrough / error mapping. Full workspace typecheck clean; native node smoke verified.
40 lines
1.8 KiB
TypeScript
40 lines
1.8 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { makeHttpClient } from '../src/client';
|
|
|
|
// Verifies the HTTP bridge: a OneBot action becomes a POST of `{ action, params }`
|
|
// to the endpoint (with Bearer auth), and the JSON envelope comes back verbatim.
|
|
// A fake fetch stands in for the network.
|
|
describe('makeHttpClient — OneBot HTTP wiring', () => {
|
|
it('POSTs {action, params} to the endpoint and returns the parsed envelope', async () => {
|
|
const calls: Array<{ url: string; body: any; auth?: string }> = [];
|
|
const fakeFetch = (async (url: unknown, init: any) => {
|
|
calls.push({
|
|
url: String(url),
|
|
body: JSON.parse(init.body as string),
|
|
auth: (init.headers as Record<string, string>)?.Authorization,
|
|
});
|
|
return new Response(JSON.stringify({ status: 'ok', retcode: 0, data: { ok: true } }), {
|
|
status: 200,
|
|
headers: { 'content-type': 'application/json' },
|
|
});
|
|
}) as unknown as typeof fetch;
|
|
|
|
const client = makeHttpClient({ endpoint: 'http://127.0.0.1:9999/', accessToken: 'tok', fetch: fakeFetch });
|
|
const env = await client.call('get_status', { x: 1 });
|
|
|
|
expect(env.status).toBe('ok');
|
|
expect(env.retcode).toBe(0);
|
|
expect(calls).toHaveLength(1);
|
|
expect(calls[0].url).toBe('http://127.0.0.1:9999/');
|
|
expect(calls[0].body.action).toBe('get_status');
|
|
expect(calls[0].body.params).toEqual({ x: 1 });
|
|
expect(calls[0].auth).toBe('Bearer tok');
|
|
});
|
|
|
|
it('propagates a transport failure as a rejection (so the tool layer can map it)', async () => {
|
|
const fakeFetch = (async () => { throw new Error('ECONNREFUSED'); }) as unknown as typeof fetch;
|
|
const client = makeHttpClient({ endpoint: 'http://127.0.0.1:9999/', fetch: fakeFetch });
|
|
await expect(client.call('get_status', {})).rejects.toThrow();
|
|
});
|
|
});
|