create-environment
Generate a new Railway environment for the linked project, with options to duplicate an existing environment and configure service variables for streamlined setup.
Instructions
Create a new Railway environment for the currently linked project. Optionally duplicate an existing environment and set service variables.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| duplicateEnvironment | No | The name of an existing environment to duplicate | |
| environmentName | Yes | The name for the new environment | |
| serviceVariables | No | Service variables to assign in the new environment (only works when duplicating) | |
| workspacePath | Yes | The path to the workspace where the environment should be created |
Implementation Reference
- src/tools/create-environment.ts:36-64 (handler)Handler function that implements the core logic of the 'create-environment' tool by invoking the Railway CLI via createRailwayEnvironment and handling responses/errors.handler: async ({ workspacePath, environmentName, duplicateEnvironment, serviceVariables, }: CreateEnvironmentOptions) => { try { const result = await createRailwayEnvironment({ workspacePath, environmentName, duplicateEnvironment, serviceVariables, }); return createToolResponse(result); } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : "Unknown error occurred"; return createToolResponse( "❌ Failed to create environment\n\n" + `**Error:** ${errorMessage}\n\n` + "**Next Steps:**\n" + "• Ensure you have a Railway project linked\n" + "• Check that the environment name is valid and unique\n" + "• Verify you have permissions to create environments in this project\n" + "• If duplicating, ensure the source environment exists\n" + "• If using service variables, ensure the service exists in the source environment", ); } },
- Zod-based input schema defining the parameters for the 'create-environment' tool: workspacePath, environmentName, optional duplicateEnvironment, and optional serviceVariables.inputSchema: { workspacePath: z .string() .describe( "The path to the workspace where the environment should be created", ), environmentName: z.string().describe("The name for the new environment"), duplicateEnvironment: z .string() .optional() .describe("The name of an existing environment to duplicate"), serviceVariables: z .array( z.object({ service: z.string().describe("The service name or UUID"), variable: z .string() .describe("The variable assignment (e.g., 'BACKEND_PORT=3000')"), }), ) .optional() .describe( "Service variables to assign in the new environment (only works when duplicating)", ), },
- src/index.ts:21-31 (registration)Dynamic registration of all exported tools from src/tools (including 'create-environment') to the MCP server via registerTool, using each tool's name, metadata, schema, and handler.Object.values(tools).forEach((tool) => { server.registerTool( tool.name, { title: tool.title, description: tool.description, inputSchema: tool.inputSchema, }, tool.handler, ); });
- src/cli/environments.ts:97-131 (helper)Core helper function that constructs and runs the Railway CLI command for creating a new environment, with support for duplicating and setting service variables; invoked by the tool handler.export const createRailwayEnvironment = async ({ workspacePath, environmentName, duplicateEnvironment, serviceVariables, }: CreateEnvironmentOptions): Promise<string> => { try { await checkRailwayCliStatus(); const result = await getLinkedProjectInfo({ workspacePath }); if (!result.success) { throw new Error(result.error); } let command = `railway environment new ${environmentName}`; if (duplicateEnvironment) { command += ` --duplicate ${duplicateEnvironment}`; } if (serviceVariables && serviceVariables.length > 0) { for (const sv of serviceVariables) { command += ` --service-variable ${sv.service} ${sv.variable}`; } } const { output } = await runRailwayCommand(command, workspacePath); return output; } catch (error: unknown) { return analyzeRailwayError( error, `railway environment new ${environmentName}`, ); } };