lax-mcp-server / tree / test/helpers.ts ยท raw (this view has old GitHub links rewritten for the mirror; raw is byte-exact)
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
import { createLaxMcpServer, type LaxMcpOptions } from '../src/server.js';

export interface Seen { url: string; method: string; body?: unknown; headers: Record<string, string> }
export type Reply = { status: number; body: unknown } | Error;

export function fakeFetch(replies: Reply[] | ((s: Seen) => Reply)) {
  const seen: Seen[] = [];
  const queue = Array.isArray(replies) ? [...replies] : null;
  const fn = (async (input: string | URL | Request, init?: RequestInit) => {
    const s: Seen = {
      url: String(input),
      method: init?.method ?? 'GET',
      body: typeof init?.body === 'string' ? JSON.parse(init.body) : undefined,
      headers: Object.fromEntries(Object.entries((init?.headers ?? {}) as Record<string, string>)),
    };
    seen.push(s);
    const r = queue ? queue.shift() ?? { status: 200, body: { ok: true } } : (replies as (s: Seen) => Reply)(s);
    if (r instanceof Error) throw r;
    return new Response(typeof r.body === 'string' ? r.body : JSON.stringify(r.body), { status: r.status, headers: { 'content-type': 'application/json' } });
  }) as typeof globalThis.fetch;
  return { fn, seen };
}

export async function connect(opts: LaxMcpOptions) {
  const server = createLaxMcpServer(opts);
  const [a, b] = InMemoryTransport.createLinkedPair();
  await server.connect(a);
  const client = new Client({ name: 'test', version: '0' });
  await client.connect(b);
  return { client, server, close: async () => { await client.close(); await server.close(); } };
}

export const textOf = (r: unknown) => ((r as { content: { text: string }[] }).content[0]?.text ?? '');

export const GOOD_SUBMIT = {
  job_id: 'a14478ee-0000-4000-8000-000000000000',
  builder: 'Test Builder',
  contact: 'secret-contact@example.com',
  repo_or_patch_url: 'https://github.com/example/repo/commit/abc',
  payout_address: '0x' + 'ab'.repeat(20),
  agent_did: 'did:pkh:eip155:8453:test',
  notes: 'hello',
};

agentgate-source: code mirror on Cloudflare Pages. All repos