railwayapp 5.5.0

Interact with Railway via CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
use rmcp::{ErrorData as McpError, model::*};

use crate::{
    client::post_graphql,
    controllers::{
        github::{resolve_repo_branch, validate_repo_name},
        project::{get_environment_instances, service_instances_in_env},
        regions::{fetch_regions_for_project, resolve_deploy_region_id},
    },
    gql::mutations,
    workspace::workspaces,
};

use super::super::handler::RailwayMcp;
use super::super::params::{
    ConnectServiceSourceParams, CreateEnvironmentParams, CreateProjectParams, CreateServiceParams,
    EnvironmentStatusParams, RemoveServiceParams, ServiceParams, UpdateServiceParams,
};

impl RailwayMcp {
    pub(crate) async fn do_create_project(
        &self,
        params: CreateProjectParams,
    ) -> Result<CallToolResult, McpError> {
        let workspace_id = match params.workspace_id {
            Some(id) => Some(id),
            None => {
                let all_workspaces = workspaces().await.map_err(|e| {
                    McpError::internal_error(format!("Failed to fetch workspaces: {e}"), None)
                })?;
                let team_workspaces: Vec<_> = all_workspaces
                    .iter()
                    .filter(|w| w.team_id().is_some())
                    .collect();
                match team_workspaces.len() {
                    0 => None,
                    1 => Some(team_workspaces[0].id().to_string()),
                    _ => {
                        let list = team_workspaces
                            .iter()
                            .map(|w| format!("- {} (id: {})", w.name(), w.id()))
                            .collect::<Vec<_>>()
                            .join("\n");
                        return Err(McpError::invalid_params(
                            format!(
                                "Multiple team workspaces found. Please specify a workspace_id:\n{list}"
                            ),
                            None,
                        ));
                    }
                }
            }
        };

        let vars = mutations::project_create::Variables {
            name: Some(params.name),
            description: params.description,
            workspace_id,
        };

        let result = post_graphql::<mutations::ProjectCreate, _>(
            &self.client,
            self.configs.get_backboard(),
            vars,
        )
        .await
        .map_err(|e| McpError::internal_error(format!("Failed to create project: {e}"), None))?;

        let project = &result.project_create;
        let env_info: Vec<String> = project
            .environments
            .edges
            .iter()
            .map(|e| format!("{} (id: {})", e.node.name, e.node.id))
            .collect();

        Ok(CallToolResult::success(vec![Content::text(format!(
            "Project created: {} (id: {})\nEnvironments: {}",
            project.name,
            project.id,
            if env_info.is_empty() {
                "none".to_string()
            } else {
                env_info.join(", ")
            }
        ))]))
    }

    pub(crate) async fn do_create_environment(
        &self,
        params: CreateEnvironmentParams,
    ) -> Result<CallToolResult, McpError> {
        let linked = self.configs.get_linked_project().await.ok();
        let project_id = params
            .project_id
            .or_else(|| linked.map(|l| l.project))
            .ok_or_else(|| {
                McpError::invalid_params(
                    "No project_id provided and no linked project. Run 'railway link' or pass a project_id.",
                    None,
                )
            })?;

        let vars = mutations::environment_create::Variables {
            project_id,
            name: params.name,
            source_id: params.source_environment_id,
            apply_changes_in_background: None,
        };

        let result = post_graphql::<mutations::EnvironmentCreate, _>(
            &self.client,
            self.configs.get_backboard(),
            vars,
        )
        .await
        .map_err(|e| {
            McpError::internal_error(format!("Failed to create environment: {e}"), None)
        })?;

        Ok(CallToolResult::success(vec![Content::text(format!(
            "Environment created: {} (id: {})",
            result.environment_create.name, result.environment_create.id
        ))]))
    }

    pub(crate) async fn do_create_service(
        &self,
        params: CreateServiceParams,
    ) -> Result<CallToolResult, McpError> {
        if params.source_image.is_some() && params.source_repo.is_some() {
            return Err(McpError::invalid_params(
                "Cannot specify both source_repo and source_image. Provide one or the other.",
                None,
            ));
        }
        if params.branch.is_some() && params.source_repo.is_none() {
            return Err(McpError::invalid_params(
                "branch can only be used with source_repo.",
                None,
            ));
        }

        let ctx = self
            .resolve_context(params.project_id, params.environment_id)
            .await?;

        let branch = if let Some(repo) = params.source_repo.as_deref() {
            validate_repo_name(repo).map_err(|e| McpError::invalid_params(e.to_string(), None))?;
            Some(
                resolve_repo_branch(&self.client, &self.configs, repo, params.branch)
                    .await
                    .map_err(|e| {
                        McpError::invalid_params(
                            format!("Failed to resolve repo branch: {e}"),
                            None,
                        )
                    })?,
            )
        } else {
            None
        };

        let source = if params.source_image.is_some() || params.source_repo.is_some() {
            Some(mutations::service_create::ServiceSourceInput {
                image: params.source_image,
                repo: params.source_repo,
            })
        } else {
            None
        };

        let vars = mutations::service_create::Variables {
            name: params.name,
            project_id: ctx.project_id,
            environment_id: ctx.environment_id,
            source,
            branch,
            variables: None,
        };

        let result = post_graphql::<mutations::ServiceCreate, _>(
            &self.client,
            self.configs.get_backboard(),
            vars,
        )
        .await
        .map_err(|e| McpError::internal_error(format!("Failed to create service: {e}"), None))?;

        Ok(CallToolResult::success(vec![Content::text(format!(
            "Service created: {} (id: {})",
            result.service_create.name, result.service_create.id
        ))]))
    }

    pub(crate) async fn do_connect_service_source(
        &self,
        params: ConnectServiceSourceParams,
    ) -> Result<CallToolResult, McpError> {
        if params.source_image.is_some() == params.source_repo.is_some() {
            return Err(McpError::invalid_params(
                "Provide exactly one of source_repo or source_image.",
                None,
            ));
        }
        if params.branch.is_some() && params.source_repo.is_none() {
            return Err(McpError::invalid_params(
                "branch can only be used with source_repo.",
                None,
            ));
        }

        let ctx = self
            .resolve_service_context(params.project_id, params.service_id, params.environment_id)
            .await?;

        let (repo, branch, image) = if let Some(repo) = params.source_repo {
            validate_repo_name(&repo).map_err(|e| McpError::invalid_params(e.to_string(), None))?;
            let branch = resolve_repo_branch(&self.client, &self.configs, &repo, params.branch)
                .await
                .map_err(|e| {
                    McpError::invalid_params(format!("Failed to resolve repo branch: {e}"), None)
                })?;
            (Some(repo), Some(branch), None)
        } else {
            (None, None, params.source_image)
        };

        let result = post_graphql::<mutations::ServiceConnect, _>(
            &self.client,
            self.configs.get_backboard(),
            mutations::service_connect::Variables {
                id: ctx.service_id.clone(),
                input: mutations::service_connect::ServiceConnectInput {
                    repo: repo.clone(),
                    branch: branch.clone(),
                    image: image.clone(),
                },
            },
        )
        .await
        .map_err(|e| {
            McpError::internal_error(format!("Failed to connect service source: {e}"), None)
        })?;

        let source = match (&repo, &branch, &image) {
            (Some(repo), Some(branch), _) => format!("{repo}@{branch}"),
            (_, _, Some(image)) => image.clone(),
            _ => "source".to_string(),
        };

        Ok(CallToolResult::success(vec![Content::text(format!(
            "Connected service source: {} (id: {}) -> {}",
            result.service_connect.name, result.service_connect.id, source
        ))]))
    }

    pub(crate) async fn do_disconnect_service_source(
        &self,
        params: ServiceParams,
    ) -> Result<CallToolResult, McpError> {
        let ctx = self
            .resolve_service_context(params.project_id, params.service_id, params.environment_id)
            .await?;

        let result = post_graphql::<mutations::ServiceDisconnect, _>(
            &self.client,
            self.configs.get_backboard(),
            mutations::service_disconnect::Variables {
                id: ctx.service_id.clone(),
            },
        )
        .await
        .map_err(|e| {
            McpError::internal_error(format!("Failed to disconnect service source: {e}"), None)
        })?;

        Ok(CallToolResult::success(vec![Content::text(format!(
            "Disconnected service source: {} (id: {})",
            result.service_disconnect.name, result.service_disconnect.id
        ))]))
    }

    pub(crate) async fn do_remove_service(
        &self,
        params: RemoveServiceParams,
    ) -> Result<CallToolResult, McpError> {
        let ctx = self
            .resolve_service_context(params.project_id, params.service_id, params.environment_id)
            .await?;

        let vars = mutations::service_delete::Variables {
            environment_id: ctx.environment_id,
            service_id: ctx.service_id,
        };

        post_graphql::<mutations::ServiceDelete, _>(
            &self.client,
            self.configs.get_backboard(),
            vars,
        )
        .await
        .map_err(|e| McpError::internal_error(format!("Failed to remove service: {e}"), None))?;

        Ok(CallToolResult::success(vec![Content::text(
            "Service removed successfully.".to_string(),
        )]))
    }

    pub(crate) async fn do_update_service(
        &self,
        params: UpdateServiceParams,
    ) -> Result<CallToolResult, McpError> {
        let ctx = self
            .resolve_service_context(params.project_id, params.service_id, params.environment_id)
            .await?;

        let restart_policy_type =
            params
                .restart_policy_type
                .map(|s| match s.to_uppercase().as_str() {
                    "ALWAYS" => mutations::service_instance_update::RestartPolicyType::ALWAYS,
                    "NEVER" => mutations::service_instance_update::RestartPolicyType::NEVER,
                    "ON_FAILURE" => {
                        mutations::service_instance_update::RestartPolicyType::ON_FAILURE
                    }
                    other => mutations::service_instance_update::RestartPolicyType::Other(
                        other.to_string(),
                    ),
                });

        let region = match params.region {
            Some(region_input) => {
                let regions =
                    fetch_regions_for_project(&self.client, &self.configs, Some(&ctx.project_id))
                        .await
                        .map_err(|e| {
                            McpError::internal_error(format!("Failed to fetch regions: {e}"), None)
                        })?;
                Some(
                    resolve_deploy_region_id(&regions, &region_input)
                        .map_err(|e| McpError::invalid_params(e.to_string(), None))?,
                )
            }
            None => None,
        };

        let input = mutations::service_instance_update::ServiceInstanceUpdateInput {
            build_command: params.build_command,
            start_command: params.start_command,
            num_replicas: params.num_replicas,
            healthcheck_path: params.health_check_path,
            healthcheck_timeout: params.healthcheck_timeout,
            sleep_application: params.sleep_application,
            root_directory: params.root_directory,
            cron_schedule: params.cron_schedule,
            dockerfile_path: params.dockerfile_path,
            restart_policy_type,
            restart_policy_max_retries: params.restart_policy_max_retries,
            pre_deploy_command: params.pre_deploy_command,
            region,
            railway_config_file: params.railway_config_file,
            watch_patterns: params.watch_patterns,
            ..Default::default()
        };

        let vars = mutations::service_instance_update::Variables {
            service_id: ctx.service_id,
            environment_id: Some(ctx.environment_id),
            input,
        };

        let result = post_graphql::<mutations::ServiceInstanceUpdate, _>(
            &self.client,
            self.configs.get_backboard(),
            vars,
        )
        .await
        .map_err(|e| McpError::internal_error(format!("Failed to update service: {e}"), None))?;

        if result.service_instance_update {
            Ok(CallToolResult::success(vec![Content::text(
                "Service updated successfully.".to_string(),
            )]))
        } else {
            Ok(CallToolResult::success(vec![Content::text(
                "Service settings are already up to date (no changes applied).".to_string(),
            )]))
        }
    }

    pub(crate) async fn do_environment_status(
        &self,
        params: EnvironmentStatusParams,
    ) -> Result<CallToolResult, McpError> {
        let ctx = self
            .resolve_context(params.project_id, params.environment_id)
            .await?;

        // Find the environment in the project
        let env = ctx
            .project
            .environments
            .edges
            .iter()
            .find(|e| e.node.id == ctx.environment_id)
            .ok_or_else(|| {
                McpError::internal_error("Environment not found in project data.".to_string(), None)
            })?;

        let mut output = format!("## Environment: {} ({})\n\n", env.node.name, env.node.id);
        output.push_str("Service | Status | Active Deployments | Latest Deploy\n");
        output.push_str("--------|--------|-------------------|---------------\n");

        let environment_instances =
            get_environment_instances(&self.client, &self.configs, &ctx.project_id, &env.node.id)
                .await
                .map_err(|e| {
                    McpError::internal_error(
                        format!("Failed to get environment instances: {e}"),
                        None,
                    )
                })?;
        let service_instances = service_instances_in_env(&environment_instances);

        for si_edge in service_instances {
            let node = &si_edge.node;
            let status = node
                .latest_deployment
                .as_ref()
                .map(|d| format!("{:?}", d.status))
                .unwrap_or_else(|| "No deployment".to_string());
            let active = node.active_deployments.len();
            let deploy_time = node
                .latest_deployment
                .as_ref()
                .map(|d| d.created_at.to_string())
                .unwrap_or_else(|| "-".to_string());

            output.push_str(&format!(
                "{} | {} | {} | {}\n",
                node.service_name, status, active, deploy_time
            ));
        }

        if service_instances.is_empty() {
            output.push_str("No services in this environment.\n");
        }

        Ok(CallToolResult::success(vec![Content::text(output)]))
    }
}