unicommand 0.0.4 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -164,6 +164,51 @@ The generated shim runs `src/cli.ts`, sets the package program-name env var, and
164
164
 
165
165
  </div>
166
166
 
167
+ <div align="center">
168
+
169
+ <details>
170
+ <summary>Show MCP server options</summary>
171
+ <br />
172
+
173
+ <div align="left">
174
+
175
+ Bind values to the whole MCP server process, such as the account a server acts as:
176
+
177
+ ```ts
178
+ import { startPackageCommandMcpServer } from 'unicommand';
179
+
180
+ await startPackageCommandMcpServer({
181
+ importMetaUrl: import.meta.url,
182
+ serverOptions: [{ name: 'account', description: 'Account email', required: true }],
183
+ instructions: ({ account }) => `This server acts as ${account}.`,
184
+ getContext: ({ account }) => createContext(account),
185
+ });
186
+ ```
187
+
188
+ - A value comes from `--account <value>` or, when absent, from `<PACKAGE>_ACCOUNT` (for example `GSHEETS_ACCOUNT`). Set `env` on the option to use another variable.
189
+ - A bound option disappears from every tool input schema and is injected into each call. A call that passes a different value fails.
190
+ - A missing required option stops the server at startup with a message on stderr.
191
+
192
+ Choose where each command option appears:
193
+
194
+ - `mcp: false` keeps an option CLI only, such as an output format or a local export path.
195
+ - `mcp: 'required'` makes an option required in the MCP schema while the CLI keeps it optional.
196
+
197
+ Run one MCP tool command without starting the server by appending its name and flags. The JSON result goes to stdout; failures exit non-zero with a message on stderr:
198
+
199
+ ```sh
200
+ my-mcp --account me@example.com auth status
201
+ my-mcp --account me@example.com auth begin --redirect-uri http://127.0.0.1:4000/callback
202
+ ```
203
+
204
+ The server negotiates the protocol version, answers `ping`, handles requests concurrently, reports tool failures with `isError: true` and unknown tools as JSON-RPC errors, and only emits standard tool annotations. Side effects and confirmation needs are added to the tool description.
205
+
206
+ </div>
207
+
208
+ </details>
209
+
210
+ </div>
211
+
167
212
  ## 🛠 Development
168
213
 
169
214
  <div align="center">
@@ -185,6 +230,7 @@ Run checks:
185
230
  ```sh
186
231
  pnpm check:fix
187
232
  pnpm typecheck
233
+ pnpm test
188
234
  pnpm build
189
235
  pnpm knip
190
236
  ```
package/dist/index.d.ts CHANGED
@@ -23,6 +23,7 @@ type CommandOptionDefinition = {
23
23
  description: string;
24
24
  aliases?: readonly string[];
25
25
  config?: CreateOptionCommandOpts;
26
+ mcp?: boolean | 'required';
26
27
  negated?: boolean;
27
28
  schema?: JsonSchemaProperty;
28
29
  value?: string;
@@ -94,6 +95,7 @@ declare const commandDefinitionToInputSchema: (definition: CommandDefinition) =>
94
95
  additionalProperties: boolean;
95
96
  };
96
97
  declare const commandOptionSynopsis: (option: CommandOptionDefinition) => string;
98
+ declare const optionInputName: (option: CommandOptionDefinition) => string;
97
99
 
98
100
  type ProgramHelpData = {
99
101
  binName: string;
@@ -106,6 +108,29 @@ declare const setProgramHelpData: (program: Program, data: ProgramHelpData) => v
106
108
  declare const isGlobalHelpRequest: (args: readonly string[]) => boolean;
107
109
  declare const printProgramHelp: (program: Program) => Promise<void>;
108
110
 
111
+ type McpServerOptionDefinition = {
112
+ name: string;
113
+ description: string;
114
+ env?: string;
115
+ required?: boolean;
116
+ };
117
+ type McpServerOptionValues = Record<string, string>;
118
+ type McpServerArgv = {
119
+ flags: Record<string, string>;
120
+ positionals: string[];
121
+ };
122
+ declare const parseMcpServerArgv: (argv: readonly string[]) => McpServerArgv;
123
+ declare const mcpServerOptionEnvName: (envPrefix: string, option: McpServerOptionDefinition) => string;
124
+ declare const resolveMcpServerOptions: ({ argvFlags, definitions, env, envPrefix, }: {
125
+ argvFlags: Record<string, string>;
126
+ definitions: readonly McpServerOptionDefinition[];
127
+ env: NodeJS.ProcessEnv;
128
+ envPrefix: string;
129
+ }) => McpServerOptionValues;
130
+ declare const withoutMcpServerOptionFlags: (flags: Record<string, string>, definitions: readonly McpServerOptionDefinition[]) => {
131
+ [k: string]: string;
132
+ };
133
+
109
134
  type McpToolDefinition = {
110
135
  name: string;
111
136
  description: string;
@@ -116,8 +141,6 @@ type McpToolDefinition = {
116
141
  destructiveHint?: boolean;
117
142
  idempotentHint?: boolean;
118
143
  openWorldHint?: boolean;
119
- requiresConfirmation?: boolean;
120
- sideEffects?: string[];
121
144
  };
122
145
  };
123
146
  type JsonRpcRequest = {
@@ -159,6 +182,8 @@ type CommandResourceRoute = {
159
182
  };
160
183
  type CreateMcpServerOptions = {
161
184
  name: string;
185
+ version?: string;
186
+ instructions?: string;
162
187
  tools: McpToolDefinition[];
163
188
  resources?: McpResourceDefinition[];
164
189
  resourceTemplates?: McpResourceTemplateDefinition[];
@@ -170,13 +195,21 @@ type CreateSingleToolMcpServerOptions = {
170
195
  tool: McpToolDefinition;
171
196
  callTool: (args: Record<string, unknown>) => Promise<unknown> | unknown;
172
197
  };
198
+ type McpServerContextFactory<TContext> = (serverOptions: McpServerOptionValues) => Promise<TContext> | TContext;
199
+ type McpServerInstructions = string | ((serverOptions: McpServerOptionValues) => string);
173
200
  type CreateCommandMcpServerOptions<TContext> = {
201
+ argv?: readonly string[];
174
202
  commandsDir?: string;
175
- getContext?: () => Promise<TContext> | TContext;
203
+ env?: NodeJS.ProcessEnv;
204
+ envPrefix?: string;
205
+ getContext?: McpServerContextFactory<TContext>;
176
206
  importCommandModule?: CommandModuleImporter;
177
207
  importMetaUrl: string;
208
+ instructions?: McpServerInstructions;
178
209
  name: string;
179
210
  prefix?: string;
211
+ serverOptions?: readonly McpServerOptionDefinition[];
212
+ version?: string;
180
213
  };
181
214
  type CreatePackageCommandMcpServerOptions<TContext> = Omit<CreateCommandMcpServerOptions<TContext>, 'name' | 'prefix'> & {
182
215
  mcpServerPrefix?: string;
@@ -191,6 +224,7 @@ type CommandMcpRoute = {
191
224
  };
192
225
  type CommandMcpRouteFactory<TContext> = (getContext: () => Promise<TContext> | TContext) => CommandMcpRoute;
193
226
  type CreateCommandMcpRouterOptions = {
227
+ boundOptions?: McpServerOptionValues;
194
228
  prefix?: string;
195
229
  routes: readonly CommandMcpRoute[];
196
230
  };
@@ -339,12 +373,26 @@ declare const generateReadmeCommandDocs: ({ binName, commandDefinitions, command
339
373
  declare const formatCommandDocs: (commands: readonly CommandDefinition[], binName: string) => string;
340
374
 
341
375
  declare const loadCommandMcpRoutes: <TContext>(commandsDir: string, getContext?: () => Promise<TContext> | TContext, importCommandModule?: CommandModuleImporter) => Promise<CommandMcpRoute[]>;
342
- declare const createCommandMcpServer: <TContext>({ commandsDir, getContext, importCommandModule, importMetaUrl, name, prefix, }: CreateCommandMcpServerOptions<TContext>) => Promise<{
376
+ declare const createCommandMcpServer: <TContext>({ argv, commandsDir, env, envPrefix, getContext, importCommandModule, importMetaUrl, instructions, name, prefix, serverOptions, version, }: CreateCommandMcpServerOptions<TContext>) => Promise<{
377
+ boundOptions: McpServerOptionValues;
378
+ command: string | undefined;
379
+ runCommand: (commandName: string, flags?: Record<string, string>) => Promise<unknown>;
380
+ commandFlags: {
381
+ [k: string]: string;
382
+ };
383
+ handleRequest: (request: JsonRpcRequest) => Promise<void>;
343
384
  start: () => Promise<void>;
344
385
  }>;
386
+ declare const runMcpCommandOnce: (routes: readonly CommandMcpRoute[], commandName: string, flags: Record<string, string>, { boundOptions }: {
387
+ boundOptions: McpServerOptionValues;
388
+ }) => Promise<unknown>;
345
389
  declare const startCommandMcpServer: <TContext>(options: CreateCommandMcpServerOptions<TContext>) => Promise<void>;
346
- declare const startPackageCommandMcpServer: <TContext>({ importMetaUrl, mcpServerPrefix, name, prefix, ...options }: CreatePackageCommandMcpServerOptions<TContext>) => Promise<void>;
390
+ declare const startPackageCommandMcpServer: <TContext>({ envPrefix, importMetaUrl, mcpServerPrefix, name, prefix, version, ...options }: CreatePackageCommandMcpServerOptions<TContext>) => Promise<void>;
347
391
 
392
+ declare class McpProtocolError extends Error {
393
+ readonly code: number;
394
+ constructor(code: number, message: string);
395
+ }
348
396
  declare const assertToolName: (expectedName: string, actualName: string | undefined) => void;
349
397
  declare const throwUnknownTool: (name: string | undefined) => never;
350
398
 
@@ -364,14 +412,14 @@ declare const resourceResult: (params: unknown, data: unknown) => {
364
412
  };
365
413
 
366
414
  declare const createCommandMcpRoute: ({ command, handler, inputSchema, outputSchema, }: CommandMcpRoute) => CommandMcpRoute;
367
- declare const commandMcpRouteToTool: (route: CommandMcpRoute, prefix?: string) => {
415
+ declare const commandMcpRouteToTool: (route: CommandMcpRoute, prefix?: string, boundOptions?: McpServerOptionValues) => {
368
416
  name: string;
369
417
  description: string;
370
- inputSchema: z.core.ZodStandardJSONSchemaPayload<z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>;
418
+ inputSchema: z.core.ZodStandardJSONSchemaPayload<ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>;
371
419
  outputSchema: {
372
420
  type: string;
373
421
  properties: {
374
- data: z.core.ZodStandardJSONSchemaPayload<z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>;
422
+ data: z.core.ZodStandardJSONSchemaPayload<ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>;
375
423
  progress: {
376
424
  type: string;
377
425
  properties: {
@@ -419,20 +467,20 @@ declare const commandMcpRouteToTool: (route: CommandMcpRoute, prefix?: string) =
419
467
  destructiveHint: boolean;
420
468
  idempotentHint: boolean;
421
469
  openWorldHint: boolean;
422
- requiresConfirmation: boolean;
423
- sideEffects: string[] | undefined;
424
470
  } | undefined;
425
471
  };
426
472
  declare const commandNameToMcpToolName: (name: string, prefix?: string) => string;
427
- declare const createCommandMcpRouter: ({ prefix, routes }: CreateCommandMcpRouterOptions) => {
473
+ declare const commandMcpInputSchema: (route: CommandMcpRoute, boundOptions?: McpServerOptionValues) => ZodType;
474
+ declare const commandMcpInput: (route: CommandMcpRoute, args: Record<string, unknown>, boundOptions?: McpServerOptionValues) => Record<string, unknown>;
475
+ declare const createCommandMcpRouter: ({ boundOptions, prefix, routes, }: CreateCommandMcpRouterOptions) => {
428
476
  tools: {
429
477
  name: string;
430
478
  description: string;
431
- inputSchema: z.core.ZodStandardJSONSchemaPayload<z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>;
479
+ inputSchema: z.core.ZodStandardJSONSchemaPayload<ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>;
432
480
  outputSchema: {
433
481
  type: string;
434
482
  properties: {
435
- data: z.core.ZodStandardJSONSchemaPayload<z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>;
483
+ data: z.core.ZodStandardJSONSchemaPayload<ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>;
436
484
  progress: {
437
485
  type: string;
438
486
  properties: {
@@ -480,8 +528,6 @@ declare const createCommandMcpRouter: ({ prefix, routes }: CreateCommandMcpRoute
480
528
  destructiveHint: boolean;
481
529
  idempotentHint: boolean;
482
530
  openWorldHint: boolean;
483
- requiresConfirmation: boolean;
484
- sideEffects: string[] | undefined;
485
531
  } | undefined;
486
532
  }[];
487
533
  resources: McpResourceDefinition[];
@@ -552,9 +598,12 @@ declare const emptyMcpInputSchema: {
552
598
  };
553
599
 
554
600
  declare const createSingleToolMcpServer: ({ serverName, tool, callTool, }: CreateSingleToolMcpServerOptions) => {
601
+ handleRequest: (request: JsonRpcRequest) => Promise<void>;
555
602
  start: () => Promise<void>;
556
603
  };
557
- declare const createMcpServer: ({ name, tools, resources, resourceTemplates, callTool, readResource, }: CreateMcpServerOptions) => {
604
+ declare const negotiateMcpProtocolVersion: (params: unknown) => string;
605
+ declare const createMcpServer: ({ name, version, instructions, tools, resources, resourceTemplates, callTool, readResource, }: CreateMcpServerOptions) => {
606
+ handleRequest: (request: JsonRpcRequest) => Promise<void>;
558
607
  start: () => Promise<void>;
559
608
  };
560
609
 
@@ -619,4 +668,4 @@ type DevCommandShimConfig = {
619
668
  declare const installCommandShim: (binDir: string, config: DevCommandShimConfig) => void;
620
669
  declare const getBinDir: (envName: string) => string;
621
670
 
622
- export { type CliConfig, type CommandAdapterExport, type CommandAdaptersReturn, type CommandArgumentDefinition, type CommandCompletionDefinition, type CommandDefinition, type CommandExamplesDefinition, type CommandMcpMetadata, type CommandMcpResourceMetadata, type CommandMcpRoute, type CommandMcpRouteFactory, type CommandMcpToolMetadata, type CommandModule, type CommandModuleImporter, type CommandOptionDefinition, type CommandRegistrar, type CommandRegistrarContext, type CommandResourceRoute, type CompletionConfig, type CreateCommandMcpRouterOptions, type CreateCommandMcpServerOptions, type CreateCommandProgramOptions, type CreateMcpServerOptions, type CreatePackageCommandMcpServerOptions, type CreateSingleToolMcpServerOptions, DEFAULT_COMMANDS_DIR, type DefineCommandDefinition, type DevCommandShimConfig, type GenerateReadmeCommandDocsOptions, type JsonRpcRequest, type JsonSchemaProperty, type McpResourceDefinition, type McpResourceTemplateDefinition, type McpResultEnvelope, type McpToolDefinition, PROGRAM_NAME_ENV_SUFFIX, type PackageBinEntry, type PackageCommandMetadata, type PackageJson, PackageManager, type RegisterCliCommandsFromDirectoryOptions, type RegisterParentCommandsFromDefinitionsOptions, type RegisterUpdateCommandOptions, type ResourceReadParams, type ShellCompletionSnippet, type ToolCallParams, assertToolName, commandActionInput, commandDefinitionToInputSchema, commandDefinitionToZodInputSchema, commandDefinitionsFromModules, commandExportsFromModules, commandInputString, commandInputValue, commandMcpRouteToResourceRoute, commandMcpRouteToTool, commandNameToMcpToolName, commandOptionSynopsis, commandResourceInput, commandResourceRouteToResource, commandResourceRouteToResourceTemplate, createCliRunner, createCommandAdapters, createCommandCliRunner, createCommandMcpRoute, createCommandMcpRouter, createCommandMcpServer, createCommandProgram, createMcpServer, createPackageCommandCliRunner, createSingleToolMcpServer, defineCommand, emptyMcpInputSchema, formatCommandDocs, generateReadmeCommandDocs, getBinDir, getPackageManagerFromPath, getProgramBin, getProgramConstructor, installCommandShim, installDevCommand, installPackageDevCommand, installPackageDevCommands, isCommandAdapterExport, isCommandMcpResourceMetadata, isCommandMcpToolMetadata, isDirectRun, isGlobalHelpRequest, isResourceTemplate, loadCommandMcpRoutes, loadCommandModules, loadCommandModulesFromFiles, mcpToolOutputSchema, normalizeCommandInputAliases, packageBinEntry, packageCommandMetadata, packageEnvPrefix, packageRoot, printProgramHelp, readPackageJson, registerCliCommandsFromDirectory, registerCommand, registerCompletionCommand, registerParentCommandsFromDefinitions, registerUpdateCommand, resourceResult, setProgramHelpData, shortPackageName, startCommandMcpServer, startPackageCommandMcpServer, throwUnknownTool, uninstallDevCommand, uninstallPackageDevCommand, uninstallPackageDevCommands };
671
+ export { type CliConfig, type CommandAdapterExport, type CommandAdaptersReturn, type CommandArgumentDefinition, type CommandCompletionDefinition, type CommandDefinition, type CommandExamplesDefinition, type CommandMcpMetadata, type CommandMcpResourceMetadata, type CommandMcpRoute, type CommandMcpRouteFactory, type CommandMcpToolMetadata, type CommandModule, type CommandModuleImporter, type CommandOptionDefinition, type CommandRegistrar, type CommandRegistrarContext, type CommandResourceRoute, type CompletionConfig, type CreateCommandMcpRouterOptions, type CreateCommandMcpServerOptions, type CreateCommandProgramOptions, type CreateMcpServerOptions, type CreatePackageCommandMcpServerOptions, type CreateSingleToolMcpServerOptions, DEFAULT_COMMANDS_DIR, type DefineCommandDefinition, type DevCommandShimConfig, type GenerateReadmeCommandDocsOptions, type JsonRpcRequest, type JsonSchemaProperty, McpProtocolError, type McpResourceDefinition, type McpResourceTemplateDefinition, type McpResultEnvelope, type McpServerArgv, type McpServerContextFactory, type McpServerInstructions, type McpServerOptionDefinition, type McpServerOptionValues, type McpToolDefinition, PROGRAM_NAME_ENV_SUFFIX, type PackageBinEntry, type PackageCommandMetadata, type PackageJson, PackageManager, type RegisterCliCommandsFromDirectoryOptions, type RegisterParentCommandsFromDefinitionsOptions, type RegisterUpdateCommandOptions, type ResourceReadParams, type ShellCompletionSnippet, type ToolCallParams, assertToolName, commandActionInput, commandDefinitionToInputSchema, commandDefinitionToZodInputSchema, commandDefinitionsFromModules, commandExportsFromModules, commandInputString, commandInputValue, commandMcpInput, commandMcpInputSchema, commandMcpRouteToResourceRoute, commandMcpRouteToTool, commandNameToMcpToolName, commandOptionSynopsis, commandResourceInput, commandResourceRouteToResource, commandResourceRouteToResourceTemplate, createCliRunner, createCommandAdapters, createCommandCliRunner, createCommandMcpRoute, createCommandMcpRouter, createCommandMcpServer, createCommandProgram, createMcpServer, createPackageCommandCliRunner, createSingleToolMcpServer, defineCommand, emptyMcpInputSchema, formatCommandDocs, generateReadmeCommandDocs, getBinDir, getPackageManagerFromPath, getProgramBin, getProgramConstructor, installCommandShim, installDevCommand, installPackageDevCommand, installPackageDevCommands, isCommandAdapterExport, isCommandMcpResourceMetadata, isCommandMcpToolMetadata, isDirectRun, isGlobalHelpRequest, isResourceTemplate, loadCommandMcpRoutes, loadCommandModules, loadCommandModulesFromFiles, mcpServerOptionEnvName, mcpToolOutputSchema, negotiateMcpProtocolVersion, normalizeCommandInputAliases, optionInputName, packageBinEntry, packageCommandMetadata, packageEnvPrefix, packageRoot, parseMcpServerArgv, printProgramHelp, readPackageJson, registerCliCommandsFromDirectory, registerCommand, registerCompletionCommand, registerParentCommandsFromDefinitions, registerUpdateCommand, resolveMcpServerOptions, resourceResult, runMcpCommandOnce, setProgramHelpData, shortPackageName, startCommandMcpServer, startPackageCommandMcpServer, throwUnknownTool, uninstallDevCommand, uninstallPackageDevCommand, uninstallPackageDevCommands, withoutMcpServerOptionFlags };