@ithena-one/mcp-governance
Version:
Governance layer (Identity, RBAC, Credentials, Audit, Logging, Tracing) for Model Context Protocol (MCP) servers.
52 lines (51 loc) • 2.73 kB
TypeScript
import { GovernedRequestHandlerExtra } from '../types.js';
/**
* Wraps a handler function to ensure params are present before the handler is executed.
* If params are missing, throws a standard MCP InvalidParams error.
*
* @template T The expected type of the params object
* @param handler A function that receives the params object as its first argument
* @returns A wrapped handler that guarantees params exists
*
* @example
* const myHandler = withSafeParams<{ name: string }>(async (params, req, extra) => {
* // Safe to use params.name here without checking
* return { greeting: `Hello ${params.name}` };
* });
*/
export declare function withSafeParams<T = Record<string, any>>(handler: (params: T, req: any, extra: GovernedRequestHandlerExtra) => Promise<any>): (req: any, extra: GovernedRequestHandlerExtra) => Promise<any>;
/**
* Wraps a handler function to ensure params are present, or use a fallback value if missing.
* Unlike withSafeParams, this never throws for missing params.
*
* @template T The expected type of the params object
* @param fallbackParams The fallback params object to use if params are missing
* @param handler A function that receives the params object as its first argument
* @returns A wrapped handler that guarantees params exists
*
* @example
* const myHandler = withFallbackParams<{ name: string }>(
* { name: "Anonymous" },
* async (params, req, extra) => {
* // params.name will be "Anonymous" if the client didn't provide it
* return { greeting: `Hello ${params.name}` };
* }
* );
*/
export declare function withFallbackParams<T = Record<string, any>>(fallbackParams: T, handler: (params: T, req: any, extra: GovernedRequestHandlerExtra) => Promise<any>): (req: any, extra: GovernedRequestHandlerExtra) => Promise<any>;
/**
* Attempts to recover params from the operation context if they're missing from the request.
* This is a more advanced utility that tries to restore the original params that might have
* been lost during transmission.
*
* @template T The expected type of the params object
* @param handler A function that receives the params object as its first argument
* @returns A wrapped handler that attempts to recover params from context
*
* @example
* const myHandler = withRecoveredParams<{ name: string }>(async (params, req, extra) => {
* // params will be recovered from operationContext.mcpMessage if possible
* return { greeting: `Hello ${params.name}` };
* });
*/
export declare function withRecoveredParams<T = Record<string, any>>(handler: (params: T | undefined, req: any, extra: GovernedRequestHandlerExtra) => Promise<any>): (req: any, extra: GovernedRequestHandlerExtra) => Promise<any>;