@mseep/railway-mcp
Version:
Model Context Protocol server for Railway.app - Enables AI agents to manage Railway infrastructure through natural language
85 lines (84 loc) • 2.73 kB
JavaScript
import { BaseApiClient } from './base-client.js';
import { DeploymentRepository } from './repository/deployment.repo.js';
import { DomainRepository } from './repository/domain.repo.js';
import { ProjectRepository } from './repository/project.repo.js';
import { ServiceRepository } from './repository/service.repo.js';
import { TcpProxyRepository } from './repository/tcpProxy.repo.js';
import { VariableRepository } from './repository/variable.repo.js';
import { VolumeRepository } from './repository/volume.repo.js';
export class RailwayApiClient extends BaseApiClient {
deployments;
domains;
projects;
services;
tcpProxies;
variables;
volumes;
initialized = false;
constructor() {
super();
this.deployments = new DeploymentRepository(this);
this.domains = new DomainRepository(this);
this.projects = new ProjectRepository(this);
this.services = new ServiceRepository(this);
this.tcpProxies = new TcpProxyRepository(this);
this.variables = new VariableRepository(this);
this.volumes = new VolumeRepository(this);
}
async initialize() {
if (this.initialized) {
return;
}
// Initialize with environment token if available
const envToken = process.env.RAILWAY_API_TOKEN;
if (envToken) {
console.error('Initializing with environment token:', envToken);
try {
this.token = envToken;
await this.validateToken();
console.error('Successfully initialized with environment token');
}
catch (error) {
console.error('Failed to initialize with environment token:', error instanceof Error ? error.message : 'Unknown error');
this.token = null;
}
}
else {
console.error('No environment token found');
}
this.initialized = true;
}
async request(query, variables) {
return super.request(query, variables);
}
async setToken(token) {
this.token = token;
if (token) {
await this.validateToken();
}
}
getToken() {
return super.getToken();
}
async validateToken() {
const query = `
query {
projects {
edges {
node {
id
}
}
}
}
`;
try {
await super.request(query);
}
catch (error) {
throw new Error('Invalid API token. Please check your token and try again.');
}
}
}
// Initialize and export the singleton instance
export const railwayClient = new RailwayApiClient();