variable_copy
Copy environment variables from one Railway environment to another for migration, configuration sharing, or duplication workflows.
Instructions
[WORKFLOW] Copy variables from one environment to another
⚡️ Best for: ✓ Environment migration ✓ Configuration sharing ✓ Environment duplication
⚠️ Not for: × Single variable updates (use variable_set) × Temporary configuration changes
→ Prerequisites: service_list
→ Alternatives: variable_set
→ Next steps: deployment_trigger, service_restart
→ Related: variable_list, service_update
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| projectId | Yes | ID of the project | |
| sourceEnvironmentId | Yes | ID of the source environment (usually obtained from project_info) | |
| targetEnvironmentId | Yes | ID of the target environment (usually obtained from project_info) | |
| serviceId | No | ID of the service (optional, if omitted copies shared variables) | |
| overwrite | No | Whether to overwrite existing variables in the target environment |
Implementation Reference
- src/tools/variable.tool.ts:130-161 (registration)Registers the 'variable_copy' MCP tool using createTool, providing formatted description, input schema, and handler function that delegates to VariableService.copyVariablescreateTool( "variable_copy", formatToolDescription({ type: 'WORKFLOW', description: "Copy variables from one environment to another", bestFor: [ "Environment migration", "Configuration sharing", "Environment duplication" ], notFor: [ "Single variable updates (use variable_set)", "Temporary configuration changes" ], relations: { prerequisites: ["service_list"], nextSteps: ["deployment_trigger", "service_restart"], alternatives: ["variable_set"], related: ["variable_list", "service_update"] } }), { projectId: z.string().describe("ID of the project"), sourceEnvironmentId: z.string().describe("ID of the source environment (usually obtained from project_info)"), targetEnvironmentId: z.string().describe("ID of the target environment (usually obtained from project_info)"), serviceId: z.string().optional().describe("ID of the service (optional, if omitted copies shared variables)"), overwrite: z.boolean().optional().default(false).describe("Whether to overwrite existing variables in the target environment") }, async ({ projectId, sourceEnvironmentId, targetEnvironmentId, serviceId, overwrite = false }) => { return variableService.copyVariables(projectId, sourceEnvironmentId, targetEnvironmentId, serviceId, overwrite); } )
- src/tools/variable.tool.ts:151-157 (schema)Zod input schema defining parameters for the variable_copy tool: projectId, sourceEnvironmentId, targetEnvironmentId, optional serviceId, and optional overwrite flag{ projectId: z.string().describe("ID of the project"), sourceEnvironmentId: z.string().describe("ID of the source environment (usually obtained from project_info)"), targetEnvironmentId: z.string().describe("ID of the target environment (usually obtained from project_info)"), serviceId: z.string().optional().describe("ID of the service (optional, if omitted copies shared variables)"), overwrite: z.boolean().optional().default(false).describe("Whether to overwrite existing variables in the target environment") },
- src/tools/variable.tool.ts:158-160 (handler)Handler function for the variable_copy tool, which invokes VariableService.copyVariables with the provided parametersasync ({ projectId, sourceEnvironmentId, targetEnvironmentId, serviceId, overwrite = false }) => { return variableService.copyVariables(projectId, sourceEnvironmentId, targetEnvironmentId, serviceId, overwrite); }
- Core helper method implementing the variable copying logic: retrieves variables from source and target environments, filters based on overwrite flag, performs bulk upsert, and returns success/error responsesasync copyVariables(projectId: string, sourceEnvironmentId: string, targetEnvironmentId: string, serviceId?: string, overwrite: boolean = false) { try { // Get variables from source environment const sourceVars = await this.client.variables.getVariables(projectId, sourceEnvironmentId, serviceId); if (Object.keys(sourceVars).length === 0) { return createSuccessResponse({ text: "No variables found in source environment to copy.", data: { copied: 0 } }); } // Get variables from target environment const targetVars = await this.client.variables.getVariables(projectId, targetEnvironmentId, serviceId); // If not overwriting, filter out variables that already exist in target const varsToSet = overwrite ? sourceVars : Object.fromEntries( Object.entries(sourceVars).filter(([key]) => !(key in targetVars)) ); if (Object.keys(varsToSet).length === 0) { return createSuccessResponse({ text: "All variables already exist in target environment.", data: { copied: 0 } }); } // Bulk update the variables await this.bulkUpsertVariables(projectId, targetEnvironmentId, varsToSet, serviceId); const variableType = serviceId ? "service variables" : "shared environment variables"; return createSuccessResponse({ text: `Successfully copied ${Object.keys(varsToSet).length} ${variableType} to target environment`, data: { copied: Object.keys(varsToSet).length } }); } catch (error) { return createErrorResponse(`Error copying variables: ${formatError(error)}`); } }