config.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Configuration manager for the Claude client
  3. */
  4. import { readFileSync, existsSync } from 'fs';
  5. import { join } from 'path';
  6. import { MCPServerConfig } from './types.js';
  7. import { McpConfigManager } from './mcpConfigManager.js';
  8. export class Config {
  9. private static envVars: Map<string, string> = new Map();
  10. /**
  11. * Load environment variables from .env file
  12. */
  13. static load(repoPath: string): void {
  14. const envPath = join(repoPath, '.env');
  15. if (!existsSync(envPath)) {
  16. return;
  17. }
  18. try {
  19. const content = readFileSync(envPath, 'utf-8');
  20. const lines = content.split('\n');
  21. for (const line of lines) {
  22. const trimmed = line.trim();
  23. // Skip comments and empty lines
  24. if (!trimmed || trimmed.startsWith('#')) {
  25. continue;
  26. }
  27. const match = trimmed.match(/^([^=]+)=(.*)$/);
  28. if (match) {
  29. const key = match[1].trim();
  30. const value = match[2].trim();
  31. this.envVars.set(key, value);
  32. // Also set in process.env if not already set
  33. if (!process.env[key]) {
  34. process.env[key] = value;
  35. }
  36. }
  37. }
  38. } catch (error) {
  39. console.error(`Warning: Failed to load .env file: ${error}`);
  40. }
  41. }
  42. /**
  43. * Get environment variable value
  44. */
  45. static get(key: string, defaultValue?: string): string {
  46. return this.envVars.get(key) || process.env[key] || defaultValue || '';
  47. }
  48. /**
  49. * Get the path to gogs-mcp server
  50. */
  51. static getGogsMcpPath(): string {
  52. return this.get('GOGS_MCP_PATH', '/data/gogs-mcp/dist/index.js');
  53. }
  54. /**
  55. * Generate MCP configuration for a repository
  56. *
  57. * Priority:
  58. * 1. Repository-specific config: ~/.config/agent-manager/mcp/{owner}/{repo}.json
  59. * 2. Global default config: ~/.config/agent-manager/mcp/default.json
  60. * 3. Hardcoded fallback (gogs-mcp only)
  61. */
  62. static generateMcpConfig(repoPath: string, owner?: string, repo?: string): MCPServerConfig {
  63. const gogsMcpPath = this.getGogsMcpPath();
  64. // If owner and repo are provided, try to load per-repository config
  65. if (owner && repo) {
  66. return McpConfigManager.getMcpConfig(owner, repo, gogsMcpPath);
  67. }
  68. // Fallback to hardcoded config
  69. return {
  70. mcpServers: {
  71. gogs: {
  72. type: 'stdio',
  73. command: 'node',
  74. args: [gogsMcpPath],
  75. env: {}
  76. }
  77. }
  78. };
  79. }
  80. }