Skip to content

Commit 3526f0f

Browse files
committed
refactor: improve MCP server option validation and error messages
- Add environment name validation for --env-only flag - Simplify environment validation logic (remove redundant devOnly check) - Standardize error messages across all tools - Add getAllowedEnvironments() helper method for consistent messaging Signed-off-by: Andy <[email protected]>
1 parent 5f6f635 commit 3526f0f

File tree

5 files changed

+26
-23
lines changed

5 files changed

+26
-23
lines changed

packages/cli-v3/src/commands/mcp.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ export async function mcpCommand(options: McpCommandOptions) {
131131
let envOnly: string[] | undefined;
132132
if (options.envOnly) {
133133
envOnly = options.envOnly.split(',').map(env => env.trim());
134+
135+
// Validate environment names
136+
const validEnvironments = ['dev', 'staging', 'prod', 'preview'];
137+
const invalidEnvs = envOnly.filter(env => !validEnvironments.includes(env));
138+
if (invalidEnvs.length > 0) {
139+
logger.error(`Error: Invalid environment(s): ${invalidEnvs.join(', ')}`);
140+
logger.error(`Valid environments are: ${validEnvironments.join(', ')}`);
141+
process.exit(1);
142+
}
134143
} else if (options.devOnly) {
135144
// For backward compatibility, convert devOnly to envOnly
136145
envOnly = ['dev'];

packages/cli-v3/src/mcp/context.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,17 +189,19 @@ export class McpContext {
189189
}
190190

191191
public isEnvironmentAllowed(environment: string): boolean {
192-
// If envOnly is specified, use that
192+
// If envOnly is specified, use that (devOnly is already converted to envOnly)
193193
if (this.options.envOnly && this.options.envOnly.length > 0) {
194194
return this.options.envOnly.includes(environment);
195195
}
196196

197-
// For backward compatibility, check devOnly
198-
if (this.options.devOnly) {
199-
return environment === "dev";
200-
}
201-
202-
// If neither is specified, all environments are allowed
197+
// If no restrictions, all environments are allowed
203198
return true;
204199
}
200+
201+
public getAllowedEnvironments(): string {
202+
if (this.options.envOnly && this.options.envOnly.length > 0) {
203+
return this.options.envOnly.join(", ");
204+
}
205+
return "all environments";
206+
}
205207
}

packages/cli-v3/src/mcp/tools/deploys.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ export const deployTool = {
2020

2121
// Check if the deployment target environment is allowed
2222
if (!ctx.isEnvironmentAllowed(input.environment)) {
23-
const allowedEnvs = ctx.options.envOnly?.join(", ") || "dev";
2423
return respondWithError(
25-
`This MCP server is restricted to the following environments: ${allowedEnvs}. You cannot deploy to ${input.environment}.`
24+
`Cannot deploy to ${input.environment} environment. This MCP server is restricted to: ${ctx.getAllowedEnvironments()}`
2625
);
2726
}
2827

@@ -121,9 +120,8 @@ export const listDeploysTool = {
121120
ctx.logger?.log("calling list_deploys", { input });
122121

123122
if (!ctx.isEnvironmentAllowed(input.environment)) {
124-
const allowedEnvs = ctx.options.envOnly?.join(", ") || "dev";
125123
return respondWithError(
126-
`This MCP server is restricted to the following environments: ${allowedEnvs}. You tried to access the ${input.environment} environment.`
124+
`Cannot access ${input.environment} environment. This MCP server is restricted to: ${ctx.getAllowedEnvironments()}`
127125
);
128126
}
129127

packages/cli-v3/src/mcp/tools/runs.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ export const getRunDetailsTool = {
1313
ctx.logger?.log("calling get_run_details", { input });
1414

1515
if (!ctx.isEnvironmentAllowed(input.environment)) {
16-
const allowedEnvs = ctx.options.envOnly?.join(", ") || "dev";
1716
return respondWithError(
18-
`This MCP server is restricted to the following environments: ${allowedEnvs}. You tried to access the ${input.environment} environment.`
17+
`Cannot access ${input.environment} environment. This MCP server is restricted to: ${ctx.getAllowedEnvironments()}`
1918
);
2019
}
2120

@@ -71,9 +70,8 @@ export const waitForRunToCompleteTool = {
7170
ctx.logger?.log("calling wait_for_run_to_complete", { input });
7271

7372
if (!ctx.isEnvironmentAllowed(input.environment)) {
74-
const allowedEnvs = ctx.options.envOnly?.join(", ") || "dev";
7573
return respondWithError(
76-
`This MCP server is restricted to the following environments: ${allowedEnvs}. You tried to access the ${input.environment} environment.`
74+
`Cannot access ${input.environment} environment. This MCP server is restricted to: ${ctx.getAllowedEnvironments()}`
7775
);
7876
}
7977

@@ -125,9 +123,8 @@ export const cancelRunTool = {
125123
ctx.logger?.log("calling cancel_run", { input });
126124

127125
if (!ctx.isEnvironmentAllowed(input.environment)) {
128-
const allowedEnvs = ctx.options.envOnly?.join(", ") || "dev";
129126
return respondWithError(
130-
`This MCP server is restricted to the following environments: ${allowedEnvs}. You tried to access the ${input.environment} environment.`
127+
`Cannot access ${input.environment} environment. This MCP server is restricted to: ${ctx.getAllowedEnvironments()}`
131128
);
132129
}
133130

@@ -166,9 +163,8 @@ export const listRunsTool = {
166163
ctx.logger?.log("calling list_runs", { input });
167164

168165
if (!ctx.isEnvironmentAllowed(input.environment)) {
169-
const allowedEnvs = ctx.options.envOnly?.join(", ") || "dev";
170166
return respondWithError(
171-
`This MCP server is restricted to the following environments: ${allowedEnvs}. You tried to access the ${input.environment} environment.`
167+
`Cannot access ${input.environment} environment. This MCP server is restricted to: ${ctx.getAllowedEnvironments()}`
172168
);
173169
}
174170

packages/cli-v3/src/mcp/tools/tasks.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ export const getCurrentWorker = {
1212
ctx.logger?.log("calling get_current_worker", { input });
1313

1414
if (!ctx.isEnvironmentAllowed(input.environment)) {
15-
const allowedEnvs = ctx.options.envOnly?.join(", ") || "dev";
1615
return respondWithError(
17-
`This MCP server is restricted to the following environments: ${allowedEnvs}. You tried to access the ${input.environment} environment.`
16+
`Cannot access ${input.environment} environment. This MCP server is restricted to: ${ctx.getAllowedEnvironments()}`
1817
);
1918
}
2019

@@ -92,9 +91,8 @@ export const triggerTaskTool = {
9291
ctx.logger?.log("calling trigger_task", { input });
9392

9493
if (!ctx.isEnvironmentAllowed(input.environment)) {
95-
const allowedEnvs = ctx.options.envOnly?.join(", ") || "dev";
9694
return respondWithError(
97-
`This MCP server is restricted to the following environments: ${allowedEnvs}. You tried to access the ${input.environment} environment.`
95+
`Cannot access ${input.environment} environment. This MCP server is restricted to: ${ctx.getAllowedEnvironments()}`
9896
);
9997
}
10098

0 commit comments

Comments
 (0)