sh-mcp/packages/shared/src/registry.ts
Adam Moussa 0d1fefb326
Some checks are pending
deploy / deploy (push) Waiting to run
Phase 0b slice: monorepo scaffold + @sh-mcp/shared core + integration packages (#2)
* Phase 0b slice: monorepo scaffold + shared core + integration packages

The 0a-INDEPENDENT code slice (one-shot via af-0b-package-slice workflow: Haiku
scaffold + Sonnet packages, Sonnet fix-to-green). Nothing deploys; no CDK/servers.

- Monorepo scaffold: npm workspaces, strict TS (NodeNext), vitest (80% gate),
  eslint 9 flat config, prettier; ci.yaml/deploy.yaml callers (Node 24, enable-qemu).
- @sh-mcp/shared: transport-agnostic core — Scope/AuthContext/ToolDef, ToolRegistry,
  redact()+maskValue() (PII), OpenAPI 3.1 generator. AUTH STUBBED behind an AuthProvider
  interface (TODO auth-layer-0a); JWT/aud/client_id/JWKS/deny-list deferred per design.md §2.
- 9 integration packages (qbo, google-maps, internal-data, payments, knowledge-base,
  gmail, calendar, tasks, reminders): tools against shared, external deps mocked behind
  injected client interfaces; finance handlers call redact().

Verified green: tsc -b clean, vitest 245/245, eslint 0 errors. Auth mechanism intentionally
deferred until the 0a spike resolves it (G16/§0.4).

* Complete Cognito auth provider + Phase 1 build brief

Finish the WIP CognitoAuthProvider (client_id allow-list as audience
boundary, finance TTL ceiling, deny-list, scope-prefix stripping) with
its test suite, and check in docs/build-plan-phase-1.md so the Phase 1
work has its governing brief in-tree (design.md §2.5).

* ci: disable cdk synth for Phase 0b (no CDK app yet)

The reusable ci-typescript-cdk workflow defaults run-cdk-synth: true, but
the Phase 0b package scaffold has no cdk.json or stacks, so cdk synth fails
with '--app is required'. Disable it here; Phase 1 re-enables it with the
server CDK stubs.
2026-06-26 12:42:17 -04:00

89 lines
2.6 KiB
TypeScript

/**
* In-memory tool registry.
*
* Packages call `defineTool()` to create a typed ToolDef, then
* `ToolRegistry.register()` to publish it. Transport adapters (OpenAPI,
* MCP) consume the registry via `list()` / `get()`.
*
* There is no global singleton registry exported here — each server
* instantiates its own registry so tests stay isolated.
*/
import type { ToolDef } from './types.js';
// ---------------------------------------------------------------------------
// defineTool — identity helper that preserves full generic types
// ---------------------------------------------------------------------------
/**
* Wrap a tool definition to get TypeScript inference on `I` and `O` while
* returning a value that satisfies `ToolDef<I, O>`.
*
* Usage:
* ```ts
* export const searchInbox = defineTool({
* name: 'search-inbox',
* tier: 'ops',
* requiredScope: 'gmail:self',
* // ...
* handler: async (input: SearchInboxInput, ctx) => { ... },
* });
* ```
*/
export function defineTool<I, O>(def: ToolDef<I, O>): ToolDef<I, O> {
return def;
}
// ---------------------------------------------------------------------------
// ToolRegistry
// ---------------------------------------------------------------------------
/**
* A typed, in-memory registry of tool definitions.
*
* Tool names must be unique across a registry instance. Attempting to
* register a duplicate name throws synchronously so misconfigurations are
* caught at server startup, not at request time.
*/
export class ToolRegistry {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private readonly tools = new Map<string, ToolDef<any, any>>();
/**
* Register a tool. Throws if a tool with the same name is already registered.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
register<I, O>(def: ToolDef<I, O>): this {
if (this.tools.has(def.name)) {
throw new Error(
`ToolRegistry: duplicate tool name "${def.name}". ` +
'Each tool name must be unique within a registry.',
);
}
this.tools.set(def.name, def);
return this;
}
/**
* Return all registered tool definitions in insertion order.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
list(): ToolDef<any, any>[] {
return [...this.tools.values()];
}
/**
* Look up a tool by name. Returns `undefined` if not found.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
get(name: string): ToolDef<any, any> | undefined {
return this.tools.get(name);
}
/**
* The number of tools currently registered.
*/
get size(): number {
return this.tools.size;
}
}