T-180: M2.1p — PostgreSQL composition root for the MCP copilot: createPostgresResearchCopilotServer wiring one injected executor into the symbol-search, trial-ref, findings, knowledge-reader and exchange-calendar adapters in libs/research/src/mcp/adapters/postgres-copilot.ts (injected store, library-card reader, session-date port, clock and id source; no new deps, no live Postgres, no entrypoint/bin)

This commit is contained in:
Studio 2026-12-02 16:34:12 -05:00
parent 0fa33eadcc
commit bc0f2c30a2
4 changed files with 1168 additions and 0 deletions

View file

@ -0,0 +1,173 @@
/**
* PostgreSQL composition root for the MCP research copilot server.
*
* Wires a single injected {@link PostgresResearchCopilotExecutor} into all
* PostgreSQL-backed MCP adapters (`search_symbols`, `file_finding`,
* `read_knowledge`, `event_study` listing calendars, and `trial_ledger`) with
* one authoritative Vault cutoff (`vaultContext.vaultCutoffTs`).
*
* Dependency-free (no `pg`, no `node:*`, no bare `process`): SQL execution,
* market data store, library-card reader, session-date port, ID generator, and
* clock are all injected by the caller.
*/
import {
type ResearchCopilotMcpServerOptions,
createResearchCopilotMcpServer,
} from '../copilot-server';
import type { McpServer } from '../server';
import type { KnowledgePort } from '../tools/read-knowledge';
import {
type EarlyCloseOverrides,
type SessionDatesPort,
createExchangeCalendarResolver,
createPostgresListingCalendarPort,
} from './exchange-calendars';
import {
createPostgresFindingWritePort,
createPostgresKnowledgeReaders,
} from './postgres-findings';
import { createPostgresSymbolSearchPort } from './postgres-symbol-search';
import { createPostgresTrialRefPort } from './postgres-trial-ref';
/**
* Structural `pg.Pool` / `pg.PoolClient`-compatible query interface shared by
* all PostgreSQL-backed MCP copilot adapters and `trial_ledger`.
*
* Assignable without casts to `SymbolSearchExecutor`, `TrialRefExecutor`,
* `PostgresFindingsExecutor`, `ListingCalendarExecutor`, and
* `TrialLedgerExecutor`.
*/
export interface PostgresResearchCopilotExecutor {
query<Row = Record<string, unknown>>(
text: string,
params?: readonly unknown[]
): Promise<{ readonly rows: readonly Row[] }>;
}
/**
* Composition options for {@link createPostgresResearchCopilotServer}.
*
* Uses `vaultContext.vaultCutoffTs` as the single authoritative Vault cutoff
* across all Vault-fenced tools and `trial_ledger` visibility lookups.
*/
export interface PostgresResearchCopilotServerOptions
extends Pick<
ResearchCopilotMcpServerOptions,
'name' | 'version' | 'store' | 'vaultContext' | 'principalId'
> {
readonly executor: PostgresResearchCopilotExecutor;
/** Optional PostgreSQL schema name (`^[a-z_][a-z0-9_]*$`); defaults to the executor's `search_path`. */
readonly schema?: string;
readonly listLibraryCards: KnowledgePort['listLibraryCards'];
readonly sessionDates: SessionDatesPort;
readonly earlyCloses?: EarlyCloseOverrides;
readonly newId: () => string;
readonly now: () => Date | number;
}
function validateOptions(options: PostgresResearchCopilotServerOptions): void {
if (typeof options !== 'object' || options === null) {
throw new Error(
'Invalid `options`: expected a PostgresResearchCopilotServerOptions object.'
);
}
if (
typeof options.executor !== 'object' ||
options.executor === null ||
typeof options.executor.query !== 'function'
) {
throw new Error(
'Invalid `executor`: expected an object with a callable query(text, params?) method.'
);
}
if (typeof options.vaultContext !== 'object' || options.vaultContext === null) {
throw new Error('Invalid `vaultContext`: expected a VaultFenceContext object.');
}
if (typeof options.listLibraryCards !== 'function') {
throw new Error('Invalid `listLibraryCards`: expected a callable function.');
}
if (
typeof options.sessionDates !== 'object' ||
options.sessionDates === null ||
typeof options.sessionDates.listSessionDates !== 'function'
) {
throw new Error(
'Invalid `sessionDates`: expected an object with a callable listSessionDates(query) method.'
);
}
if (typeof options.newId !== 'function') {
throw new Error('Invalid `newId`: expected a callable function.');
}
if (typeof options.now !== 'function') {
throw new Error('Invalid `now`: expected a callable function.');
}
}
/**
* Composes all six research copilot MCP tools into a single {@link McpServer}
* backed by one shared PostgreSQL executor and one authoritative Vault cutoff.
*
* Schema-aware adapters (`search_symbols`, `trial_ledger` visibility lookup,
* `findings`, `provider_caveats`, and listing-to-exchange calendars) qualify
* their tables with `options.schema` when supplied; `trial_ledger` writes and
* cluster DSR reads resolve through the executor's `search_path`.
*/
export function createPostgresResearchCopilotServer(
options: PostgresResearchCopilotServerOptions
): McpServer {
validateOptions(options);
const pgAdapterOptions =
options.schema === undefined
? { executor: options.executor }
: { executor: options.executor, schema: options.schema };
const symbolSearchPort = createPostgresSymbolSearchPort(pgAdapterOptions);
const trialRefPort = createPostgresTrialRefPort({
...pgAdapterOptions,
vaultCutoffTs: options.vaultContext.vaultCutoffTs,
});
const findingWritePort = createPostgresFindingWritePort(pgAdapterOptions);
const knowledgeReaders = createPostgresKnowledgeReaders(pgAdapterOptions);
const listingCalendars = createPostgresListingCalendarPort(pgAdapterOptions);
const resolveExchangeCalendars = createExchangeCalendarResolver(
options.earlyCloses === undefined
? { listingCalendars, sessionDates: options.sessionDates }
: {
listingCalendars,
sessionDates: options.sessionDates,
earlyCloses: options.earlyCloses,
}
);
const knowledgePort: KnowledgePort = {
listLibraryCards: options.listLibraryCards,
...knowledgeReaders,
};
return createResearchCopilotMcpServer({
name: options.name,
version: options.version,
store: options.store,
vaultContext: options.vaultContext,
ledger: options.executor,
principalId: options.principalId,
searchSymbols: {
port: symbolSearchPort,
},
eventStudy: {
resolveExchangeCalendars,
},
fileFinding: {
port: findingWritePort,
trialLookup: trialRefPort,
newId: options.newId,
now: options.now,
},
readKnowledge: {
port: knowledgePort,
},
});
}

View file

@ -12,3 +12,4 @@ export * from './tools/file-finding';
export * from './tools/query-data';
export * from './tools/read-knowledge';
export * from './tools/search-symbols';
export * from './adapters/postgres-copilot';