Add Prime Intellect client, API, and MCP server

packages/prime — a hand-written typed client, because the first-party SDK is
Python only. Deliberately narrow: PIG reads availability and nothing else, and
the key it holds should be scoped so it could not provision even if the code
tried. Rate limits are undocumented upstream, so it backs off empirically with
full jitter and honours Retry-After. Unknown fields survive in `raw` rather
than being dropped.

apps/api — Hono, with authentication and authorization kept firmly apart. A
verified JWT proves someone has an account in the identity project, which may
be shared with other applications; it does NOT prove they belong here. Access
requires a row in PIG's own users table, and a token without one gets 403
needs_profile rather than entry.

The capacity service is the business logic: availability counts sold and held
separately, so a live hold removes inventory from everyone else's availability
without inflating utilisation. Expired holds are ignored at read time, so the
numbers stay right even when the sweeper is behind. Matching treats
interconnect as a hard filter and excludes Unknown as well as Ethernet —
unverified is not the same as adequate.

apps/mcp — nine tools over stdio, so a team member drives PIG from Claude
Code, Codex, prime-agent, or a Buzz agent. It holds an API key and calls the
same HTTP API the browser does, with no database credentials, so an agent can
never reach further than the person it acts for. Results are formatted as
prose rather than raw JSON.

Theme preferences live in the database rather than localStorage, so a chosen
accent follows someone from laptop to phone. Status colours stay independent
of the accent: if "at risk" re-tinted to whatever a user picked, the signal
would be gone.

Note on the SDK import: its package exports use a `./*` wildcard whose types
entry resolves server/mcp.js to server/mcp.js.d.ts, which does not exist. The
runtime specifier must keep the .js suffix, so the types are mapped via
tsconfig paths rather than by writing an import that would fail at runtime.

Verified: all five packages typecheck; the MCP server constructs and registers
its tools.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-12 19:02:45 -07:00
parent d36762f264
commit 7aeec0c632
21 changed files with 3997 additions and 2 deletions
+66
View File
@@ -0,0 +1,66 @@
/**
* The PIG server.
*
* Serves the API and, in production, the built front end from the same origin.
* Same-origin matters more than it might appear: the browser holds its auth
* session per origin, so splitting the app across two hostnames turns sign-in
* into a loop that looks like a broken deployment.
*/
import { serve } from '@hono/node-server';
import { serveStatic } from '@hono/node-server/serve-static';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import { createDatabase } from '@pig/db';
import { createApp } from './app';
import { loadConfig } from './lib/config';
import { startPrimeSync } from './services/sync';
import { CapacityService } from './services/capacity';
const config = loadConfig();
const db = createDatabase({ url: config.DATABASE_URL });
const app = createApp(config, db);
// Serve the built SPA when it exists. Absent in development, where Vite serves
// it on its own port with hot reload.
const webDist = join(process.cwd(), 'apps/web/dist');
if (existsSync(webDist)) {
app.use('/assets/*', serveStatic({ root: './apps/web/dist' }));
app.get('*', serveStatic({ path: './apps/web/dist/index.html' }));
console.log('[pig] serving front end from', webDist);
}
const server = serve({ fetch: app.fetch, port: config.PIG_PORT }, (info) => {
console.log(`[pig] listening on http://localhost:${info.port}`);
console.log(`[pig] environment: ${config.NODE_ENV}`);
});
// Background work. Both are optional and the application is fully usable with
// neither running.
const stopSync = startPrimeSync(config, db);
const capacity = new CapacityService(db);
const holdSweeper = setInterval(
() => {
void capacity
.sweepExpiredHolds()
.then((n) => n > 0 && console.log(`[pig] released ${n} expired capacity hold(s)`))
.catch((error) => console.error('[pig] hold sweep failed', error));
},
5 * 60 * 1000,
);
/**
* Shut down cleanly so that in-flight requests finish and Postgres connections
* are returned rather than left for the server to reap.
*/
function shutdown(signal: string) {
console.log(`[pig] ${signal} received, shutting down`);
clearInterval(holdSweeper);
stopSync();
server.close(() => process.exit(0));
// Do not hang forever if a connection refuses to drain.
setTimeout(() => process.exit(1), 10_000).unref();
}
process.on('SIGTERM', () => shutdown('SIGTERM'));
process.on('SIGINT', () => shutdown('SIGINT'));