create-environment
Create a new Railway environment for your project, optionally duplicating an existing environment and setting service variables to manage configurations.
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 |
|---|---|---|---|
| workspacePath | Yes | The path to the workspace where the environment should be created | |
| environmentName | Yes | The name for the new environment | |
| duplicateEnvironment | No | The name of an existing environment to duplicate | |
| serviceVariables | No | Service variables to assign in the new environment (only works when duplicating) |
Implementation Reference
- src/tools/create-environment.ts:36-64 (handler)The handler function that implements the core logic for the 'create-environment' tool. It invokes the Railway CLI via createRailwayEnvironment and formats responses or 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", ); } },
- The Zod input schema defining parameters for the 'create-environment' tool: workspacePath, environmentName, optional duplicateEnvironment and 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 tools from './tools' (including 'create-environment') into the MCP server using server.registerTool.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)Helper function that executes the actual Railway CLI command to create the environment, used 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}`, ); } };