@lobu/gateway 3.0.8 → 3.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (219) hide show
  1. package/dist/api/platform.d.ts.map +1 -1
  2. package/dist/api/platform.js +8 -26
  3. package/dist/api/platform.js.map +1 -1
  4. package/dist/auth/mcp/proxy.d.ts +14 -0
  5. package/dist/auth/mcp/proxy.d.ts.map +1 -1
  6. package/dist/auth/mcp/proxy.js +149 -13
  7. package/dist/auth/mcp/proxy.js.map +1 -1
  8. package/dist/cli/gateway.d.ts.map +1 -1
  9. package/dist/cli/gateway.js +29 -0
  10. package/dist/cli/gateway.js.map +1 -1
  11. package/dist/cli/index.js +2 -2
  12. package/dist/cli/index.js.map +1 -1
  13. package/dist/connections/chat-instance-manager.d.ts.map +1 -1
  14. package/dist/connections/chat-instance-manager.js +2 -1
  15. package/dist/connections/chat-instance-manager.js.map +1 -1
  16. package/dist/connections/interaction-bridge.d.ts +9 -2
  17. package/dist/connections/interaction-bridge.d.ts.map +1 -1
  18. package/dist/connections/interaction-bridge.js +132 -230
  19. package/dist/connections/interaction-bridge.js.map +1 -1
  20. package/dist/connections/message-handler-bridge.d.ts.map +1 -1
  21. package/dist/connections/message-handler-bridge.js +44 -26
  22. package/dist/connections/message-handler-bridge.js.map +1 -1
  23. package/dist/interactions.d.ts +9 -43
  24. package/dist/interactions.d.ts.map +1 -1
  25. package/dist/interactions.js +10 -52
  26. package/dist/interactions.js.map +1 -1
  27. package/dist/orchestration/base-deployment-manager.js +7 -7
  28. package/dist/orchestration/base-deployment-manager.js.map +1 -1
  29. package/dist/platform/unified-thread-consumer.d.ts.map +1 -1
  30. package/dist/platform/unified-thread-consumer.js +38 -34
  31. package/dist/platform/unified-thread-consumer.js.map +1 -1
  32. package/dist/routes/public/agent.d.ts +4 -0
  33. package/dist/routes/public/agent.d.ts.map +1 -1
  34. package/dist/routes/public/agent.js +21 -0
  35. package/dist/routes/public/agent.js.map +1 -1
  36. package/dist/services/core-services.d.ts.map +1 -1
  37. package/dist/services/core-services.js +4 -0
  38. package/dist/services/core-services.js.map +1 -1
  39. package/package.json +2 -2
  40. package/src/__tests__/agent-config-routes.test.ts +0 -254
  41. package/src/__tests__/agent-history-routes.test.ts +0 -72
  42. package/src/__tests__/agent-routes.test.ts +0 -68
  43. package/src/__tests__/agent-schedules-routes.test.ts +0 -59
  44. package/src/__tests__/agent-settings-store.test.ts +0 -323
  45. package/src/__tests__/bedrock-model-catalog.test.ts +0 -40
  46. package/src/__tests__/bedrock-openai-service.test.ts +0 -157
  47. package/src/__tests__/bedrock-provider-module.test.ts +0 -56
  48. package/src/__tests__/chat-instance-manager-slack.test.ts +0 -204
  49. package/src/__tests__/chat-response-bridge.test.ts +0 -131
  50. package/src/__tests__/config-memory-plugins.test.ts +0 -92
  51. package/src/__tests__/config-request-store.test.ts +0 -127
  52. package/src/__tests__/connection-routes.test.ts +0 -144
  53. package/src/__tests__/core-services-store-selection.test.ts +0 -92
  54. package/src/__tests__/docker-deployment.test.ts +0 -1211
  55. package/src/__tests__/embedded-deployment.test.ts +0 -342
  56. package/src/__tests__/grant-store.test.ts +0 -148
  57. package/src/__tests__/http-proxy.test.ts +0 -281
  58. package/src/__tests__/instruction-service.test.ts +0 -37
  59. package/src/__tests__/link-buttons.test.ts +0 -112
  60. package/src/__tests__/lobu.test.ts +0 -32
  61. package/src/__tests__/mcp-config-service.test.ts +0 -347
  62. package/src/__tests__/mcp-proxy.test.ts +0 -694
  63. package/src/__tests__/message-handler-bridge.test.ts +0 -17
  64. package/src/__tests__/model-selection.test.ts +0 -172
  65. package/src/__tests__/oauth-templates.test.ts +0 -39
  66. package/src/__tests__/platform-adapter-slack-send.test.ts +0 -114
  67. package/src/__tests__/platform-helpers-model-resolution.test.ts +0 -253
  68. package/src/__tests__/provider-inheritance.test.ts +0 -212
  69. package/src/__tests__/routes/cli-auth.test.ts +0 -337
  70. package/src/__tests__/routes/interactions.test.ts +0 -121
  71. package/src/__tests__/secret-proxy.test.ts +0 -85
  72. package/src/__tests__/session-manager.test.ts +0 -572
  73. package/src/__tests__/setup.ts +0 -133
  74. package/src/__tests__/skill-and-mcp-registry.test.ts +0 -203
  75. package/src/__tests__/slack-routes.test.ts +0 -161
  76. package/src/__tests__/system-config-resolver.test.ts +0 -75
  77. package/src/__tests__/system-message-limiter.test.ts +0 -89
  78. package/src/__tests__/system-skills-service.test.ts +0 -362
  79. package/src/__tests__/transcription-service.test.ts +0 -222
  80. package/src/__tests__/utils/rate-limiter.test.ts +0 -102
  81. package/src/__tests__/worker-connection-manager.test.ts +0 -497
  82. package/src/__tests__/worker-job-router.test.ts +0 -722
  83. package/src/api/index.ts +0 -1
  84. package/src/api/platform.ts +0 -292
  85. package/src/api/response-renderer.ts +0 -157
  86. package/src/auth/agent-metadata-store.ts +0 -168
  87. package/src/auth/api-auth-middleware.ts +0 -69
  88. package/src/auth/api-key-provider-module.ts +0 -213
  89. package/src/auth/base-provider-module.ts +0 -201
  90. package/src/auth/bedrock/provider-module.ts +0 -110
  91. package/src/auth/chatgpt/chatgpt-oauth-module.ts +0 -185
  92. package/src/auth/chatgpt/device-code-client.ts +0 -218
  93. package/src/auth/chatgpt/index.ts +0 -1
  94. package/src/auth/claude/oauth-module.ts +0 -280
  95. package/src/auth/cli/token-service.ts +0 -249
  96. package/src/auth/external/client.ts +0 -560
  97. package/src/auth/external/device-code-client.ts +0 -235
  98. package/src/auth/mcp/config-service.ts +0 -420
  99. package/src/auth/mcp/proxy.ts +0 -1086
  100. package/src/auth/mcp/string-substitution.ts +0 -17
  101. package/src/auth/mcp/tool-cache.ts +0 -90
  102. package/src/auth/oauth/base-client.ts +0 -267
  103. package/src/auth/oauth/client.ts +0 -153
  104. package/src/auth/oauth/credentials.ts +0 -7
  105. package/src/auth/oauth/providers.ts +0 -69
  106. package/src/auth/oauth/state-store.ts +0 -150
  107. package/src/auth/oauth-templates.ts +0 -179
  108. package/src/auth/provider-catalog.ts +0 -220
  109. package/src/auth/provider-model-options.ts +0 -41
  110. package/src/auth/settings/agent-settings-store.ts +0 -565
  111. package/src/auth/settings/auth-profiles-manager.ts +0 -216
  112. package/src/auth/settings/index.ts +0 -12
  113. package/src/auth/settings/model-preference-store.ts +0 -52
  114. package/src/auth/settings/model-selection.ts +0 -135
  115. package/src/auth/settings/resolved-settings-view.ts +0 -298
  116. package/src/auth/settings/template-utils.ts +0 -44
  117. package/src/auth/settings/token-service.ts +0 -88
  118. package/src/auth/system-env-store.ts +0 -98
  119. package/src/auth/user-agents-store.ts +0 -68
  120. package/src/channels/binding-service.ts +0 -214
  121. package/src/channels/index.ts +0 -4
  122. package/src/cli/gateway.ts +0 -1312
  123. package/src/cli/index.ts +0 -74
  124. package/src/commands/built-in-commands.ts +0 -80
  125. package/src/commands/command-dispatcher.ts +0 -94
  126. package/src/commands/command-reply-adapters.ts +0 -27
  127. package/src/config/file-loader.ts +0 -618
  128. package/src/config/index.ts +0 -588
  129. package/src/config/network-allowlist.ts +0 -71
  130. package/src/connections/chat-instance-manager.ts +0 -1284
  131. package/src/connections/chat-response-bridge.ts +0 -618
  132. package/src/connections/index.ts +0 -7
  133. package/src/connections/interaction-bridge.ts +0 -831
  134. package/src/connections/message-handler-bridge.ts +0 -415
  135. package/src/connections/platform-auth-methods.ts +0 -15
  136. package/src/connections/types.ts +0 -84
  137. package/src/gateway/connection-manager.ts +0 -291
  138. package/src/gateway/index.ts +0 -698
  139. package/src/gateway/job-router.ts +0 -201
  140. package/src/gateway-main.ts +0 -200
  141. package/src/index.ts +0 -41
  142. package/src/infrastructure/queue/index.ts +0 -12
  143. package/src/infrastructure/queue/queue-producer.ts +0 -148
  144. package/src/infrastructure/queue/redis-queue.ts +0 -361
  145. package/src/infrastructure/queue/types.ts +0 -133
  146. package/src/infrastructure/redis/system-message-limiter.ts +0 -94
  147. package/src/interactions/config-request-store.ts +0 -198
  148. package/src/interactions.ts +0 -363
  149. package/src/lobu.ts +0 -311
  150. package/src/metrics/prometheus.ts +0 -159
  151. package/src/modules/module-system.ts +0 -179
  152. package/src/orchestration/base-deployment-manager.ts +0 -900
  153. package/src/orchestration/deployment-utils.ts +0 -98
  154. package/src/orchestration/impl/docker-deployment.ts +0 -620
  155. package/src/orchestration/impl/embedded-deployment.ts +0 -268
  156. package/src/orchestration/impl/index.ts +0 -8
  157. package/src/orchestration/impl/k8s/deployment.ts +0 -1061
  158. package/src/orchestration/impl/k8s/helpers.ts +0 -610
  159. package/src/orchestration/impl/k8s/index.ts +0 -1
  160. package/src/orchestration/index.ts +0 -333
  161. package/src/orchestration/message-consumer.ts +0 -584
  162. package/src/orchestration/scheduled-wakeup.ts +0 -704
  163. package/src/permissions/approval-policy.ts +0 -36
  164. package/src/permissions/grant-store.ts +0 -219
  165. package/src/platform/file-handler.ts +0 -66
  166. package/src/platform/link-buttons.ts +0 -57
  167. package/src/platform/renderer-utils.ts +0 -44
  168. package/src/platform/response-renderer.ts +0 -84
  169. package/src/platform/unified-thread-consumer.ts +0 -187
  170. package/src/platform.ts +0 -318
  171. package/src/proxy/http-proxy.ts +0 -752
  172. package/src/proxy/proxy-manager.ts +0 -81
  173. package/src/proxy/secret-proxy.ts +0 -402
  174. package/src/proxy/token-refresh-job.ts +0 -143
  175. package/src/routes/internal/audio.ts +0 -141
  176. package/src/routes/internal/device-auth.ts +0 -652
  177. package/src/routes/internal/files.ts +0 -226
  178. package/src/routes/internal/history.ts +0 -69
  179. package/src/routes/internal/images.ts +0 -127
  180. package/src/routes/internal/interactions.ts +0 -84
  181. package/src/routes/internal/middleware.ts +0 -23
  182. package/src/routes/internal/schedule.ts +0 -226
  183. package/src/routes/internal/types.ts +0 -22
  184. package/src/routes/openapi-auto.ts +0 -239
  185. package/src/routes/public/agent-access.ts +0 -23
  186. package/src/routes/public/agent-config.ts +0 -675
  187. package/src/routes/public/agent-history.ts +0 -422
  188. package/src/routes/public/agent-schedules.ts +0 -296
  189. package/src/routes/public/agent.ts +0 -1086
  190. package/src/routes/public/agents.ts +0 -373
  191. package/src/routes/public/channels.ts +0 -191
  192. package/src/routes/public/cli-auth.ts +0 -896
  193. package/src/routes/public/connections.ts +0 -574
  194. package/src/routes/public/landing.ts +0 -16
  195. package/src/routes/public/oauth.ts +0 -147
  196. package/src/routes/public/settings-auth.ts +0 -104
  197. package/src/routes/public/slack.ts +0 -173
  198. package/src/routes/shared/agent-ownership.ts +0 -101
  199. package/src/routes/shared/token-verifier.ts +0 -34
  200. package/src/services/bedrock-model-catalog.ts +0 -217
  201. package/src/services/bedrock-openai-service.ts +0 -658
  202. package/src/services/core-services.ts +0 -1072
  203. package/src/services/image-generation-service.ts +0 -257
  204. package/src/services/instruction-service.ts +0 -318
  205. package/src/services/mcp-registry.ts +0 -94
  206. package/src/services/platform-helpers.ts +0 -287
  207. package/src/services/session-manager.ts +0 -262
  208. package/src/services/settings-resolver.ts +0 -74
  209. package/src/services/system-config-resolver.ts +0 -89
  210. package/src/services/system-skills-service.ts +0 -229
  211. package/src/services/transcription-service.ts +0 -684
  212. package/src/session.ts +0 -110
  213. package/src/spaces/index.ts +0 -1
  214. package/src/spaces/space-resolver.ts +0 -17
  215. package/src/stores/in-memory-agent-store.ts +0 -403
  216. package/src/stores/redis-agent-store.ts +0 -279
  217. package/src/utils/public-url.ts +0 -44
  218. package/src/utils/rate-limiter.ts +0 -94
  219. package/tsconfig.json +0 -33
@@ -1,235 +0,0 @@
1
- import { BaseOAuth2Client } from "../oauth/base-client";
2
- import type { OAuthCredentials } from "../oauth/credentials";
3
-
4
- export const DEVICE_CODE_GRANT_TYPE =
5
- "urn:ietf:params:oauth:grant-type:device_code";
6
-
7
- export interface DeviceCodeClientConfig {
8
- clientId: string;
9
- clientSecret?: string;
10
- tokenUrl: string;
11
- deviceAuthorizationUrl: string;
12
- scope: string;
13
- /** RFC 8707 resource indicator included in token requests. */
14
- resource?: string;
15
- tokenEndpointAuthMethod?:
16
- | "none"
17
- | "client_secret_post"
18
- | "client_secret_basic";
19
- }
20
-
21
- export interface DeviceAuthorizationStartResult {
22
- deviceAuthId: string;
23
- userCode: string;
24
- verificationUri: string;
25
- verificationUriComplete?: string;
26
- interval: number;
27
- expiresIn: number;
28
- }
29
-
30
- export type DeviceAuthorizationPollResult =
31
- | {
32
- status: "pending";
33
- interval?: number;
34
- }
35
- | {
36
- status: "complete";
37
- credentials: OAuthCredentials;
38
- }
39
- | {
40
- status: "error";
41
- error: string;
42
- errorCode?: string;
43
- };
44
-
45
- interface DeviceAuthorizationResponse {
46
- device_code: string;
47
- user_code: string;
48
- verification_uri: string;
49
- verification_uri_complete?: string;
50
- expires_in: number;
51
- interval?: number;
52
- }
53
-
54
- interface DeviceTokenSuccessResponse {
55
- access_token: string;
56
- refresh_token?: string;
57
- token_type?: string;
58
- expires_in?: number;
59
- scope?: string;
60
- }
61
-
62
- interface DeviceTokenErrorResponse {
63
- error: string;
64
- error_description?: string;
65
- }
66
-
67
- export class GenericDeviceCodeClient extends BaseOAuth2Client {
68
- constructor(private readonly config: DeviceCodeClientConfig) {
69
- super("external-device-code-client");
70
- }
71
-
72
- async requestDeviceCode(): Promise<DeviceAuthorizationStartResult> {
73
- const params: Record<string, string> = {
74
- client_id: this.config.clientId,
75
- scope: this.config.scope,
76
- };
77
- if (this.config.resource) {
78
- params.resource = this.config.resource;
79
- }
80
- const { headers, body } = this.buildAuthenticatedFormBody(params);
81
-
82
- const response = await fetch(this.config.deviceAuthorizationUrl, {
83
- method: "POST",
84
- headers,
85
- body: body.toString(),
86
- });
87
-
88
- const data = await this.parseJsonResponse<
89
- DeviceAuthorizationResponse | DeviceTokenErrorResponse
90
- >(response);
91
-
92
- if (!response.ok || "error" in data) {
93
- throw new Error(
94
- this.formatOAuthError("Device authorization failed", data)
95
- );
96
- }
97
-
98
- return {
99
- deviceAuthId: data.device_code,
100
- userCode: data.user_code,
101
- verificationUri: data.verification_uri,
102
- verificationUriComplete: data.verification_uri_complete,
103
- interval: Math.max(data.interval ?? 5, 1),
104
- expiresIn: data.expires_in,
105
- };
106
- }
107
-
108
- async pollForToken(
109
- deviceAuthId: string,
110
- intervalSeconds?: number
111
- ): Promise<DeviceAuthorizationPollResult> {
112
- const params: Record<string, string> = {
113
- grant_type: DEVICE_CODE_GRANT_TYPE,
114
- device_code: deviceAuthId,
115
- client_id: this.config.clientId,
116
- };
117
- if (this.config.resource) {
118
- params.resource = this.config.resource;
119
- }
120
- const { headers, body } = this.buildAuthenticatedFormBody(params);
121
-
122
- const response = await fetch(this.config.tokenUrl, {
123
- method: "POST",
124
- headers,
125
- body: body.toString(),
126
- });
127
-
128
- const data = await this.parseJsonResponse<
129
- DeviceTokenSuccessResponse | DeviceTokenErrorResponse
130
- >(response);
131
-
132
- if (!response.ok || "error" in data) {
133
- if ("error" in data) {
134
- if (data.error === "authorization_pending") {
135
- return { status: "pending", interval: intervalSeconds };
136
- }
137
- if (data.error === "slow_down") {
138
- return {
139
- status: "pending",
140
- interval: Math.max((intervalSeconds ?? 5) + 5, 1),
141
- };
142
- }
143
- if (data.error === "expired_token" || data.error === "access_denied") {
144
- return {
145
- status: "error",
146
- error: data.error_description || data.error,
147
- errorCode: data.error,
148
- };
149
- }
150
- }
151
-
152
- throw new Error(
153
- this.formatOAuthError("Device token polling failed", data)
154
- );
155
- }
156
-
157
- const credentials = this.buildCredentials(data);
158
- return {
159
- status: "complete",
160
- credentials,
161
- };
162
- }
163
-
164
- private buildCredentials(
165
- tokenData: DeviceTokenSuccessResponse
166
- ): OAuthCredentials {
167
- return {
168
- accessToken: tokenData.access_token,
169
- refreshToken: tokenData.refresh_token,
170
- tokenType: tokenData.token_type || "Bearer",
171
- expiresAt:
172
- this.calculateExpiresAt(tokenData.expires_in) ?? Date.now() + 3_600_000,
173
- scopes: this.parseScopes(tokenData.scope),
174
- };
175
- }
176
-
177
- private buildAuthenticatedFormBody(params: Record<string, string>): {
178
- headers: Record<string, string>;
179
- body: URLSearchParams;
180
- } {
181
- const body = new URLSearchParams(params);
182
- const headers: Record<string, string> = {
183
- Accept: "application/json",
184
- "Content-Type": "application/x-www-form-urlencoded",
185
- };
186
- const authMethod = this.config.tokenEndpointAuthMethod || "none";
187
-
188
- if (authMethod === "client_secret_basic") {
189
- headers.Authorization = `Basic ${Buffer.from(
190
- `${this.config.clientId}:${this.config.clientSecret || ""}`
191
- ).toString("base64")}`;
192
- } else if (
193
- authMethod === "client_secret_post" &&
194
- this.config.clientSecret
195
- ) {
196
- body.set("client_secret", this.config.clientSecret);
197
- }
198
-
199
- return { headers, body };
200
- }
201
-
202
- private async parseJsonResponse<T>(response: Response): Promise<T> {
203
- const contentType = response.headers.get("content-type") || "";
204
- if (contentType.includes("application/json")) {
205
- return (await response.json()) as T;
206
- }
207
-
208
- const text = await response.text();
209
- try {
210
- return JSON.parse(text) as T;
211
- } catch {
212
- return { error: text || response.statusText } as T;
213
- }
214
- }
215
-
216
- private formatOAuthError(prefix: string, data: unknown): string {
217
- if (data && typeof data === "object" && "error" in data) {
218
- const record = data as {
219
- error?: unknown;
220
- error_description?: unknown;
221
- };
222
- const error =
223
- typeof record.error === "string" ? record.error : "unknown_error";
224
- const description =
225
- typeof record.error_description === "string"
226
- ? record.error_description
227
- : undefined;
228
- return description
229
- ? `${prefix}: ${error} - ${description}`
230
- : `${prefix}: ${error}`;
231
- }
232
-
233
- return prefix;
234
- }
235
- }
@@ -1,420 +0,0 @@
1
- import {
2
- type McpOAuthConfig,
3
- createLogger,
4
- verifyWorkerToken,
5
- } from "@lobu/core";
6
- import type { SystemConfigResolver } from "../../services/system-config-resolver";
7
- import type { AgentSettingsStore } from "../settings/agent-settings-store";
8
-
9
- const logger = createLogger("mcp-config-service");
10
-
11
- interface McpInput {
12
- type: "promptString";
13
- id: string;
14
- description: string;
15
- }
16
-
17
- export interface HttpMcpServerConfig {
18
- id: string;
19
- upstreamUrl: string;
20
- oauth?: McpOAuthConfig;
21
- inputs?: McpInput[];
22
- headers?: Record<string, string>;
23
- }
24
-
25
- interface WorkerMcpConfig {
26
- mcpServers: Record<string, any>;
27
- }
28
-
29
- interface McpStatus {
30
- id: string;
31
- name: string;
32
- requiresAuth: boolean;
33
- requiresInput: boolean;
34
- }
35
-
36
- interface LoadedConfig {
37
- rawServers: Record<string, any>;
38
- httpServers: Map<string, HttpMcpServerConfig>;
39
- }
40
-
41
- interface McpConfigServiceOptions {
42
- agentSettingsStore?: AgentSettingsStore;
43
- configResolver?: SystemConfigResolver;
44
- }
45
-
46
- export class McpConfigService {
47
- private cache?: LoadedConfig;
48
- private agentSettingsStore?: AgentSettingsStore;
49
- private configResolver?: SystemConfigResolver;
50
-
51
- constructor(options: McpConfigServiceOptions = {}) {
52
- this.agentSettingsStore = options.agentSettingsStore;
53
- this.configResolver = options.configResolver;
54
- logger.debug(`McpConfigService initialized`);
55
- }
56
-
57
- /**
58
- * Register additional global MCP servers (e.g. from system skills).
59
- */
60
- registerGlobalServers(servers: Record<string, any>): void {
61
- if (!this.cache) {
62
- this.cache = {
63
- rawServers: {},
64
- httpServers: new Map(),
65
- };
66
- }
67
-
68
- const normalized = normalizeConfig({ mcpServers: servers });
69
- for (const [id, raw] of Object.entries(normalized.rawServers)) {
70
- if (this.cache.rawServers[id]) continue;
71
- this.cache.rawServers[id] = raw;
72
- }
73
- for (const [id, http] of normalized.httpServers) {
74
- if (this.cache.httpServers.has(id)) continue;
75
- this.cache.httpServers.set(id, http);
76
- }
77
-
78
- logger.info(
79
- `Registered ${Object.keys(servers).length} global MCP(s) from system skills: ${Object.keys(servers).join(", ")}`
80
- );
81
- }
82
-
83
- /**
84
- * Return MCP config tailored for a worker request.
85
- */
86
- async getWorkerConfig(options: {
87
- baseUrl: string;
88
- workerToken: string;
89
- deploymentName?: string;
90
- }): Promise<WorkerMcpConfig> {
91
- const { baseUrl, workerToken } = options;
92
- const config = await this.loadConfig();
93
- const workerConfig: WorkerMcpConfig = { mcpServers: {} };
94
-
95
- const tokenData = verifyWorkerToken(workerToken);
96
- if (!tokenData) {
97
- logger.warn("Failed to verify worker token");
98
- return workerConfig;
99
- }
100
-
101
- const { userId, agentId } = tokenData;
102
- const effectiveAgentId = agentId || userId;
103
- logger.info(`Building MCP config for user ${userId}`);
104
-
105
- // Process global MCPs
106
- for (const [id, serverConfig] of Object.entries(config.rawServers)) {
107
- const cloned = cloneConfig(serverConfig);
108
- const httpServer = config.httpServers.get(id);
109
-
110
- if (httpServer) {
111
- logger.info(`Configuring global MCP ${id}: baseUrl=${baseUrl}`);
112
- cloned.url = baseUrl;
113
- cloned.type = "sse";
114
- cloned.headers = mergeHeaders(cloned.headers, workerToken, id);
115
- logger.info(
116
- `Including global MCP ${id} with URL=${cloned.url} and X-Mcp-Id header`
117
- );
118
- }
119
-
120
- workerConfig.mcpServers[id] = cloned;
121
- }
122
-
123
- // Merge per-agent MCPs from live agent settings
124
- const agentSettingsMcpServers =
125
- (await this.getAgentMcpServers(effectiveAgentId)) || {};
126
- for (const [id, serverConfig] of Object.entries(agentSettingsMcpServers)) {
127
- if (workerConfig.mcpServers[id]) {
128
- logger.warn(
129
- `Per-agent MCP ${id} skipped - global MCP with same ID exists`
130
- );
131
- continue;
132
- }
133
-
134
- const cloned = cloneConfig(serverConfig);
135
-
136
- if (cloned.enabled === false) {
137
- logger.debug(`Skipping disabled per-agent MCP ${id}`);
138
- continue;
139
- }
140
-
141
- if (cloned.url) {
142
- logger.info(`Configuring per-agent HTTP MCP ${id}: baseUrl=${baseUrl}`);
143
- cloned.originalUrl = cloned.url;
144
- cloned.url = baseUrl;
145
- cloned.type = "sse";
146
- cloned.headers = mergeHeaders(cloned.headers, workerToken, id);
147
- cloned.perAgent = true;
148
- logger.info(`Including per-agent HTTP MCP ${id}`);
149
- } else if (cloned.command) {
150
- logger.info(`Including per-agent stdio MCP ${id}: ${cloned.command}`);
151
- }
152
-
153
- workerConfig.mcpServers[id] = cloned;
154
- }
155
-
156
- if (Object.keys(agentSettingsMcpServers).length > 0) {
157
- logger.info(
158
- `Merged ${Object.keys(agentSettingsMcpServers).length} per-agent MCPs from settings for agent ${effectiveAgentId}`
159
- );
160
- }
161
-
162
- logger.info(
163
- `Returning worker config with ${Object.keys(workerConfig.mcpServers).length} MCPs for user ${userId}:`,
164
- {
165
- mcpIds: Object.keys(workerConfig.mcpServers),
166
- configs: Object.entries(workerConfig.mcpServers).map(([id, cfg]) => ({
167
- id,
168
- type: cfg.type,
169
- hasUrl: !!cfg.url,
170
- hasCommand: !!cfg.command,
171
- perAgent: cfg.perAgent || false,
172
- })),
173
- }
174
- );
175
-
176
- return workerConfig;
177
- }
178
-
179
- /**
180
- * Get status of all MCPs for a specific agent
181
- */
182
- async getMcpStatus(agentId: string): Promise<McpStatus[]> {
183
- const httpServers = await this.getAllHttpServers(agentId);
184
- const statuses: McpStatus[] = [];
185
-
186
- for (const [id, httpServer] of httpServers) {
187
- const requiresAuth = !!httpServer.oauth;
188
- const requiresInput = !!(
189
- httpServer.inputs && httpServer.inputs.length > 0
190
- );
191
-
192
- statuses.push({
193
- id,
194
- name: id,
195
- requiresAuth,
196
- requiresInput,
197
- });
198
- }
199
-
200
- return statuses;
201
- }
202
-
203
- /**
204
- * Get HTTP proxy metadata for a specific MCP server.
205
- */
206
- async getHttpServer(
207
- id: string,
208
- agentId?: string
209
- ): Promise<HttpMcpServerConfig | undefined> {
210
- const httpServers = await this.getAllHttpServers(agentId);
211
- return httpServers.get(id);
212
- }
213
-
214
- /**
215
- * Get all HTTP proxy metadata for all MCP servers.
216
- */
217
- async getAllHttpServers(
218
- agentId?: string
219
- ): Promise<Map<string, HttpMcpServerConfig>> {
220
- const config = await this.loadConfig();
221
- const merged = new Map(config.httpServers);
222
-
223
- if (agentId) {
224
- const agentMcpServers = await this.getAgentMcpServers(agentId);
225
- for (const [id, serverConfig] of Object.entries(agentMcpServers)) {
226
- if (merged.has(id)) continue;
227
- const httpServer = toHttpServerConfig(id, serverConfig);
228
- if (httpServer) {
229
- merged.set(id, httpServer);
230
- }
231
- }
232
- }
233
-
234
- return merged;
235
- }
236
-
237
- /**
238
- * Return global MCP server configs in settings-compatible format.
239
- */
240
- async getGlobalMcpServers(): Promise<
241
- Record<string, { url?: string; type?: "sse" | "stdio" }>
242
- > {
243
- const config = await this.loadConfig();
244
- const result: Record<string, { url?: string; type?: "sse" | "stdio" }> = {};
245
- for (const [id, raw] of Object.entries(config.rawServers)) {
246
- const type = raw.type === "stdio" ? ("stdio" as const) : ("sse" as const);
247
- result[id] = { url: raw.url, type };
248
- }
249
- return result;
250
- }
251
-
252
- private async getAgentMcpServers(
253
- agentId: string
254
- ): Promise<Record<string, any>> {
255
- if (!this.agentSettingsStore) {
256
- return {};
257
- }
258
-
259
- try {
260
- const settings = await this.agentSettingsStore.getSettings(agentId);
261
- return settings?.mcpServers || {};
262
- } catch (error) {
263
- logger.warn(`Failed to load per-agent MCP settings for ${agentId}`, {
264
- error,
265
- });
266
- return {};
267
- }
268
- }
269
-
270
- private async loadConfig(): Promise<LoadedConfig> {
271
- if (!this.cache) {
272
- let globalMcpServers: Record<string, any> = {};
273
- if (this.configResolver) {
274
- globalMcpServers = await this.configResolver.getGlobalMcpServers();
275
- }
276
- const normalized = normalizeConfig({ mcpServers: globalMcpServers });
277
- this.cache = normalized;
278
- }
279
- return this.cache;
280
- }
281
- }
282
-
283
- /**
284
- * Parse and validate an oauth config from raw MCP server config.
285
- * Handles backward compat: migrates top-level `resource` into `oauth.resource`,
286
- * and treats `loginUrl` presence as `oauth: {}` (requiresAuth flag).
287
- */
288
- function parseOAuthConfig(raw: any): McpOAuthConfig | undefined {
289
- const hasLoginUrl = typeof raw.loginUrl === "string";
290
- const hasOAuth = raw.oauth && typeof raw.oauth === "object";
291
-
292
- if (!hasOAuth && !hasLoginUrl && typeof raw.resource !== "string") {
293
- return undefined;
294
- }
295
-
296
- const config: McpOAuthConfig = {};
297
-
298
- if (hasOAuth) {
299
- const obj = raw.oauth;
300
- if (typeof obj.authUrl === "string") config.authUrl = obj.authUrl;
301
- if (typeof obj.tokenUrl === "string") config.tokenUrl = obj.tokenUrl;
302
- if (typeof obj.clientId === "string") config.clientId = obj.clientId;
303
- if (typeof obj.clientSecret === "string")
304
- config.clientSecret = obj.clientSecret;
305
- if (Array.isArray(obj.scopes))
306
- config.scopes = obj.scopes.filter((s: unknown) => typeof s === "string");
307
- if (typeof obj.deviceAuthorizationUrl === "string")
308
- config.deviceAuthorizationUrl = obj.deviceAuthorizationUrl;
309
- if (typeof obj.registrationUrl === "string")
310
- config.registrationUrl = obj.registrationUrl;
311
- if (typeof obj.resource === "string") config.resource = obj.resource;
312
- }
313
-
314
- // Migrate top-level resource into oauth.resource (backward compat)
315
- if (typeof raw.resource === "string" && !config.resource) {
316
- config.resource = raw.resource;
317
- }
318
-
319
- return config;
320
- }
321
-
322
- function normalizeConfig(config: { mcpServers: Record<string, any> }) {
323
- const rawServers: Record<string, any> = {};
324
- const httpServers = new Map<string, HttpMcpServerConfig>();
325
-
326
- for (const [id, serverConfig] of Object.entries(config.mcpServers)) {
327
- if (!serverConfig || typeof serverConfig !== "object") {
328
- continue;
329
- }
330
-
331
- const cloned = cloneConfig(serverConfig);
332
- rawServers[id] = cloned;
333
-
334
- if (typeof cloned.url === "string" && isHttpUrl(cloned.url)) {
335
- httpServers.set(id, {
336
- id,
337
- upstreamUrl: cloned.url,
338
- oauth: parseOAuthConfig(cloned),
339
- inputs: Array.isArray(cloned.inputs)
340
- ? cloned.inputs.filter(
341
- (input: any) =>
342
- input &&
343
- typeof input === "object" &&
344
- input.type === "promptString"
345
- )
346
- : undefined,
347
- headers:
348
- cloned.headers && typeof cloned.headers === "object"
349
- ? cloned.headers
350
- : undefined,
351
- });
352
- }
353
- }
354
-
355
- return { rawServers, httpServers };
356
- }
357
-
358
- function toHttpServerConfig(
359
- id: string,
360
- serverConfig: any
361
- ): HttpMcpServerConfig | null {
362
- if (!serverConfig || typeof serverConfig !== "object") {
363
- return null;
364
- }
365
-
366
- if (serverConfig.enabled === false) {
367
- return null;
368
- }
369
-
370
- const cloned = cloneConfig(serverConfig);
371
- if (typeof cloned.url !== "string" || !isHttpUrl(cloned.url)) {
372
- return null;
373
- }
374
-
375
- return {
376
- id,
377
- upstreamUrl: cloned.url,
378
- oauth: parseOAuthConfig(cloned),
379
- inputs: Array.isArray(cloned.inputs)
380
- ? cloned.inputs.filter(
381
- (input: any) =>
382
- input && typeof input === "object" && input.type === "promptString"
383
- )
384
- : undefined,
385
- headers:
386
- cloned.headers && typeof cloned.headers === "object"
387
- ? cloned.headers
388
- : undefined,
389
- };
390
- }
391
-
392
- function cloneConfig(config: any) {
393
- return JSON.parse(JSON.stringify(config));
394
- }
395
-
396
- function isHttpUrl(candidate: string): boolean {
397
- return candidate.startsWith("http://") || candidate.startsWith("https://");
398
- }
399
-
400
- function mergeHeaders(
401
- existingHeaders: unknown,
402
- workerToken: string,
403
- mcpId: string
404
- ): Record<string, string> {
405
- const normalized: Record<string, string> = {};
406
-
407
- if (existingHeaders && typeof existingHeaders === "object") {
408
- for (const [key, value] of Object.entries(existingHeaders as any)) {
409
- if (typeof value === "string") {
410
- normalized[key] = value;
411
- } else if (value != null) {
412
- normalized[key] = String(value);
413
- }
414
- }
415
- }
416
-
417
- normalized.Authorization = `Bearer ${workerToken}`;
418
- normalized["X-Mcp-Id"] = mcpId;
419
- return normalized;
420
- }