Extract MCP source connection logic into a reusable connectMcpSourceInternal function and expose it as a new /sources/mcp/oauth/start endpoint. The credential-complete callback now returns an HTML popup page that posts results back to the opener via postMessage. Includes an integration test for the full popup OAuth callback flow.
Unified · Split
| @@ -802,6 +802,83 @@ describe("local-executor-server", () => { · 801 unmodified lines | |||
| 802 | }), | 802 | }), |
| 803 | ); | 803 | ); |
| 804 | 804 | ||
| 805 | it.scoped("starts MCP OAuth from the sources API and completes the popup callback flow", () => | ||
| 806 | Effect.gen(function* () { | ||
| 807 | const oauthServer = yield* Effect.acquireRelease( | ||
| 808 | Effect.promise(() => startOAuthProtectedMcpServer()), | ||
| 809 | (server) => Effect.promise(() => server.close()).pipe(Effect.orDie), | ||
| 810 | ); | ||
| 811 | |||
| 812 | const server = yield* createLocalExecutorServer({ | ||
| 813 | port: 0, | ||
| 814 | localDataDir: ":memory:", | ||
| 815 | }); | ||
| 816 | |||
| 817 | const bootstrapClient = yield* createControlPlaneClient({ | ||
| 818 | baseUrl: server.baseUrl, | ||
| 819 | }); | ||
| 820 | const installation = yield* bootstrapClient.local.installation({}); | ||
| 821 | |||
| 822 | const startResponse = yield* Effect.promise(() => | ||
| 823 | fetch(`${server.baseUrl}/v1/workspaces/${encodeURIComponent(installation.workspaceId)}/sources/mcp/oauth/start`, { | ||
| 824 | method: "POST", | ||
| 825 | headers: { | ||
| 826 | "content-type": "application/json", | ||
| 827 | "x-executor-account-id": installation.accountId, | ||
| 828 | }, | ||
| 829 | body: JSON.stringify({ | ||
| 830 | endpoint: oauthServer.endpoint, | ||
| 831 | name: "Axiom", | ||
| 832 | namespace: "axiom", | ||
| 833 | transport: "auto", | ||
| 834 | }), | ||
| 835 | }), | ||
| 836 | ); | ||
| 837 | |||
| 838 | assertTrue(startResponse.ok); | ||
| 839 | const started: { | ||
| 840 | kind: "connected" | "oauth_required"; | ||
| 841 | source: { id: string; status: string; auth: { kind: string } }; | ||
| 842 | authorizationUrl?: string; | ||
| 843 | } = yield* Effect.promise(() => startResponse.json() as Promise<{ | ||
| 844 | kind: "connected" | "oauth_required"; | ||
| 845 | source: { id: string; status: string; auth: { kind: string } }; | ||
| 846 | authorizationUrl?: string; | ||
| 847 | }>); | ||
| 848 | |||
| 849 | expect(started.kind).toBe("oauth_required"); | ||
| 850 | expect(started.source.status).toBe("auth_required"); | ||
| 851 | expect(started.authorizationUrl).toBeDefined(); | ||
| 852 | |||
| 853 | const callbackResponse = yield* Effect.promise(() => | ||
| 854 | fetch(started.authorizationUrl!, { | ||
| 855 | redirect: "follow", | ||
| 856 | }), | ||
| 857 | ); | ||
| 858 | |||
| 859 | assertTrue(callbackResponse.ok); | ||
| 860 | const callbackHtml = yield* Effect.promise(() => callbackResponse.text()); | ||
| 861 | expect(callbackHtml).toContain("Source connected:"); | ||
| 862 | expect(callbackHtml).toContain("executor-v3:source-oauth-result"); | ||
| 863 | |||
| 864 | const client = yield* createControlPlaneClient({ | ||
| 865 | baseUrl: server.baseUrl, | ||
| 866 | accountId: installation.accountId, | ||
| 867 | }); | ||
| 868 | const sources = yield* client.sources.list({ | ||
| 869 | path: { | ||
| 870 | workspaceId: installation.workspaceId, | ||
| 871 | }, | ||
| 872 | }); | ||
| 873 | const source = sources.find((candidate) => candidate.id === started.source.id); | ||
| 874 | |||
| 875 | assertTrue(Boolean(source)); | ||
| 876 | |||
| 877 | expect(source?.status).toBe("connected"); | ||
| 878 | expect(source?.auth.kind).toBe("oauth2"); | ||
| 879 | }), | ||
| 880 | ); | ||
| 881 | |||
| 805 | it.scoped("returns a typed storage error when a configured MCP endpoint is invalid", () => | 882 | it.scoped("returns a typed storage error when a configured MCP endpoint is invalid", () => |
| 806 | Effect.gen(function* () { | 883 | Effect.gen(function* () { |
| 807 | const server = yield* createLocalExecutorServer({ | 884 | const server = yield* createLocalExecutorServer({ |
| @@ -2,6 +2,7 @@ import { HttpApiEndpoint, HttpApiGroup, HttpApiSchema } from "@effect/platform"; · 1 unmodified line | |||
| 2 | import { | 2 | import { |
| 3 | ExecutionInteractionIdSchema, | 3 | ExecutionInteractionIdSchema, |
| 4 | SourceAuthSchema, | 4 | SourceAuthSchema, |
| 5 | SourceAuthSessionIdSchema, | ||
| 5 | SourceIdSchema, | 6 | SourceIdSchema, |
| 6 | SourceInspectionDiscoverPayloadSchema, | 7 | SourceInspectionDiscoverPayloadSchema, |
| 7 | SourceInspectionDiscoverResultSchema, | 8 | SourceInspectionDiscoverResultSchema, |
| @@ -74,6 +75,34 @@ export const UpdateSourcePayloadSchema = Schema.Struct({ · 66 unmodified lines | |||
| 74 | 75 | ||
| 75 | export type UpdateSourcePayload = typeof UpdateSourcePayloadSchema.Type; | 76 | export type UpdateSourcePayload = typeof UpdateSourcePayloadSchema.Type; |
| 76 | 77 | ||
| 78 | export const ConnectMcpSourcePayloadSchema = Schema.Struct({ | ||
| 79 | sourceId: Schema.optional(SourceIdSchema), | ||
| 80 | name: Schema.optional(Schema.NullOr(Schema.String)), | ||
| 81 | endpoint: TrimmedNonEmptyStringSchema, | ||
| 82 | namespace: Schema.optional(Schema.NullOr(Schema.String)), | ||
| 83 | enabled: Schema.optional(Schema.Boolean), | ||
| 84 | transport: Schema.optional(SourceTransportSchema), | ||
| 85 | queryParams: Schema.optional(Schema.NullOr(StringMapSchema)), | ||
| 86 | headers: Schema.optional(Schema.NullOr(StringMapSchema)), | ||
| 87 | }); | ||
| 88 | |||
| 89 | export type ConnectMcpSourcePayload = typeof ConnectMcpSourcePayloadSchema.Type; | ||
| 90 | |||
| 91 | export const ConnectMcpSourceResultSchema = Schema.Union( | ||
| 92 | Schema.Struct({ | ||
| 93 | kind: Schema.Literal("connected"), | ||
| 94 | source: SourceSchema, | ||
| 95 | }), | ||
| 96 | Schema.Struct({ | ||
| 97 | kind: Schema.Literal("oauth_required"), | ||
| 98 | source: SourceSchema, | ||
| 99 | sessionId: SourceAuthSessionIdSchema, | ||
| 100 | authorizationUrl: Schema.String, | ||
| 101 | }), | ||
| 102 | ); | ||
| 103 | |||
| 104 | export type ConnectMcpSourceResult = typeof ConnectMcpSourceResultSchema.Type; | ||
| 105 | |||
| 77 | const workspaceIdParam = HttpApiSchema.param("workspaceId", WorkspaceIdSchema); | 106 | const workspaceIdParam = HttpApiSchema.param("workspaceId", WorkspaceIdSchema); |
| 78 | const sourceIdParam = HttpApiSchema.param("sourceId", SourceIdSchema); | 107 | const sourceIdParam = HttpApiSchema.param("sourceId", SourceIdSchema); |
| 79 | const toolPathParam = HttpApiSchema.param("toolPath", Schema.String); | 108 | const toolPathParam = HttpApiSchema.param("toolPath", Schema.String); |
| @@ -135,6 +164,16 @@ export class SourcesApi extends HttpApiGroup.make("sources") · 55 unmodified lines | |||
| 135 | .addError(ControlPlaneNotFoundError) | 164 | .addError(ControlPlaneNotFoundError) |
| 136 | .addError(ControlPlaneStorageError), | 165 | .addError(ControlPlaneStorageError), |
| 137 | ) | 166 | ) |
| 167 | .add( | ||
| 168 | HttpApiEndpoint.post("connectMcp")`/workspaces/${workspaceIdParam}/sources/mcp/oauth/start` | ||
| 169 | .setPayload(ConnectMcpSourcePayloadSchema) | ||
| 170 | .addSuccess(ConnectMcpSourceResultSchema) | ||
| 171 | .addError(ControlPlaneBadRequestError) | ||
| 172 | .addError(ControlPlaneUnauthorizedError) | ||
| 173 | .addError(ControlPlaneForbiddenError) | ||
| 174 | .addError(ControlPlaneNotFoundError) | ||
| 175 | .addError(ControlPlaneStorageError), | ||
| 176 | ) | ||
| 138 | .add( | 177 | .add( |
| 139 | HttpApiEndpoint.del("remove")`/workspaces/${workspaceIdParam}/sources/${sourceIdParam}` | 178 | HttpApiEndpoint.del("remove")`/workspaces/${workspaceIdParam}/sources/${sourceIdParam}` |
| 140 | .addSuccess(Schema.Struct({ removed: Schema.Boolean })) | 179 | .addSuccess(Schema.Struct({ removed: Schema.Boolean })) |
| @@ -163,7 +202,7 @@ export class SourcesApi extends HttpApiGroup.make("sources") · 22 unmodified lines | |||
| 163 | .add( | 202 | .add( |
| 164 | HttpApiEndpoint.get("credentialComplete")`/workspaces/${workspaceIdParam}/sources/${sourceIdParam}/credentials/oauth/complete` | 203 | HttpApiEndpoint.get("credentialComplete")`/workspaces/${workspaceIdParam}/sources/${sourceIdParam}/credentials/oauth/complete` |
| 165 | .setUrlParams(CredentialOauthCompleteUrlParamsSchema) | 204 | .setUrlParams(CredentialOauthCompleteUrlParamsSchema) |
| 166 | .addSuccess(Schema.String) | 205 | .addSuccess(HtmlSchema) |
| 167 | .addError(ControlPlaneBadRequestError) | 206 | .addError(ControlPlaneBadRequestError) |
| 168 | .addError(ControlPlaneNotFoundError) | 207 | .addError(ControlPlaneNotFoundError) |
| 169 | .addError(ControlPlaneStorageError), | 208 | .addError(ControlPlaneStorageError), |
| @@ -11,6 +11,7 @@ import * as Effect from "effect/Effect"; · 10 unmodified lines | |||
| 11 | 11 | ||
| 12 | import { requirePermission, withPolicy } from "#domain"; | 12 | import { requirePermission, withPolicy } from "#domain"; |
| 13 | import { | 13 | import { |
| 14 | connectMcpSource, | ||
| 14 | createSource, | 15 | createSource, |
| 15 | getSource, | 16 | getSource, |
| 16 | listSources, | 17 | listSources, |
| @@ -56,6 +57,12 @@ const escapeHtml = (value: string): string => · 39 unmodified lines | |||
| 56 | .replaceAll('"', """) | 57 | .replaceAll('"', """) |
| 57 | .replaceAll("'", "'"); | 58 | .replaceAll("'", "'"); |
| 58 | 59 | ||
| 60 | const serializeJsonForScript = (value: unknown): string => | ||
| 61 | JSON.stringify(value) | ||
| 62 | .replaceAll("<", "\\u003c") | ||
| 63 | .replaceAll(">", "\\u003e") | ||
| 64 | .replaceAll("&", "\\u0026"); | ||
| 65 | |||
| 59 | const credentialPageDocument = (input: { | 66 | const credentialPageDocument = (input: { |
| 60 | title: string; | 67 | title: string; |
| 61 | eyebrow: string; | 68 | eyebrow: string; |
| @@ -419,6 +426,122 @@ const credentialPageDocument = (input: { · 357 unmodified lines | |||
| 419 | </body> | 426 | </body> |
| 420 | </html>`; | 427 | </html>`; |
| 421 | 428 | ||
| 429 | const sourceOAuthPopupResultDocument = (input: { | ||
| 430 | title: string; | ||
| 431 | message: string; | ||
| 432 | state: "stored" | "cancelled"; | ||
| 433 | payload: | ||
| 434 | | { | ||
| 435 | type: "executor-v3:source-oauth-result"; | ||
| 436 | ok: true; | ||
| 437 | sourceId: Source["id"]; | ||
| 438 | } | ||
| 439 | | { | ||
| 440 | type: "executor-v3:source-oauth-result"; | ||
| 441 | ok: false; | ||
| 442 | error: string; | ||
| 443 | }; | ||
| 444 | }) => `<!doctype html> | ||
| 445 | <html lang="en"> | ||
| 446 | <head> | ||
| 447 | <meta charset="utf-8" /> | ||
| 448 | <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| 449 | <title>${escapeHtml(input.title)}</title> | ||
| 450 | <style> | ||
| 451 | :root { | ||
| 452 | --bg: #fafaf9; | ||
| 453 | --surface: #ffffff; | ||
| 454 | --text: #1c1917; | ||
| 455 | --text-secondary: #78716c; | ||
| 456 | --border: #e7e5e4; | ||
| 457 | --green: #16a34a; | ||
| 458 | --green-light: #f0fdf4; | ||
| 459 | --green-border: #bbf7d0; | ||
| 460 | --red: #dc2626; | ||
| 461 | --red-light: #fef2f2; | ||
| 462 | --red-border: #fecaca; | ||
| 463 | } | ||
| 464 | |||
| 465 | * { box-sizing: border-box; margin: 0; padding: 0; } | ||
| 466 | |||
| 467 | body { | ||
| 468 | min-height: 100vh; | ||
| 469 | display: flex; | ||
| 470 | align-items: center; | ||
| 471 | justify-content: center; | ||
| 472 | background: var(--bg); | ||
| 473 | color: var(--text); | ||
| 474 | font-family: system-ui, -apple-system, sans-serif; | ||
| 475 | padding: 24px; | ||
| 476 | } | ||
| 477 | |||
| 478 | .card { | ||
| 479 | width: 100%; | ||
| 480 | max-width: 420px; | ||
| 481 | background: var(--surface); | ||
| 482 | border: 1px solid var(--border); | ||
| 483 | border-radius: 16px; | ||
| 484 | padding: 28px; | ||
| 485 | text-align: center; | ||
| 486 | } | ||
| 487 | |||
| 488 | .status { | ||
| 489 | display: inline-flex; | ||
| 490 | align-items: center; | ||
| 491 | justify-content: center; | ||
| 492 | padding: 6px 12px; | ||
| 493 | border-radius: 999px; | ||
| 494 | font-size: 12px; | ||
| 495 | font-weight: 600; | ||
| 496 | margin-bottom: 14px; | ||
| 497 | } | ||
| 498 | |||
| 499 | .status[data-state='stored'] { | ||
| 500 | background: var(--green-light); | ||
| 501 | border: 1px solid var(--green-border); | ||
| 502 | color: var(--green); | ||
| 503 | } | ||
| 504 | |||
| 505 | .status[data-state='cancelled'] { | ||
| 506 | background: var(--red-light); | ||
| 507 | border: 1px solid var(--red-border); | ||
| 508 | color: var(--red); | ||
| 509 | } | ||
| 510 | |||
| 511 | h1 { | ||
| 512 | font-size: 22px; | ||
| 513 | line-height: 1.2; | ||
| 514 | margin-bottom: 10px; | ||
| 515 | } | ||
| 516 | |||
| 517 | p { | ||
| 518 | color: var(--text-secondary); | ||
| 519 | font-size: 14px; | ||
| 520 | line-height: 1.6; | ||
| 521 | } | ||
| 522 | </style> | ||
| 523 | </head> | ||
| 524 | <body> | ||
| 525 | <main class="card"> | ||
| 526 | <div class="status" data-state="${escapeHtml(input.state)}">${escapeHtml(input.state === "stored" ? "connected" : "failed")}</div> | ||
| 527 | <h1>${escapeHtml(input.title)}</h1> | ||
| 528 | <p>${escapeHtml(input.message)}</p> | ||
| 529 | </main> | ||
| 530 | <script> | ||
| 531 | (() => { | ||
| 532 | const payload = ${serializeJsonForScript(input.payload)}; | ||
| 533 | try { | ||
| 534 | if (window.opener) { | ||
| 535 | window.opener.postMessage(payload, window.location.origin); | ||
| 536 | } | ||
| 537 | } finally { | ||
| 538 | window.setTimeout(() => window.close(), 60); | ||
| 539 | } | ||
| 540 | })(); | ||
| 541 | </script> | ||
| 542 | </body> | ||
| 543 | </html>`; | ||
| 544 | |||
| 422 | const htmlResponse = (html: string, status = 200) => | 545 | const htmlResponse = (html: string, status = 200) => |
| 423 | HttpServerResponse.html(html).pipe( | 546 | HttpServerResponse.html(html).pipe( |
| 424 | HttpServerResponse.setStatus(status), | 547 | HttpServerResponse.setStatus(status), |
| @@ -560,6 +683,16 @@ export const ControlPlaneSourcesLive = HttpApiBuilder.group( · 135 unmodified lines | |||
| 560 | ), | 683 | ), |
| 561 | ), | 684 | ), |
| 562 | ) | 685 | ) |
| 686 | .handle("connectMcp", ({ path, payload }) => | ||
| 687 | withWorkspaceRequestActor("sources.connectMcp", path.workspaceId, () => | ||
| 688 | withPolicy(requireWriteSources(path.workspaceId))( | ||
| 689 | connectMcpSource({ | ||
| 690 | workspaceId: path.workspaceId, | ||
| 691 | payload, | ||
| 692 | }), | ||
| 693 | ), | ||
| 694 | ), | ||
| 695 | ) | ||
| 563 | .handle("remove", ({ path }) => | 696 | .handle("remove", ({ path }) => |
| 564 | withWorkspaceRequestActor("sources.remove", path.workspaceId, () => | 697 | withWorkspaceRequestActor("sources.remove", path.workspaceId, () => |
| 565 | withPolicy(requireWriteSources(path.workspaceId))( | 698 | withPolicy(requireWriteSources(path.workspaceId))( |
| @@ -687,7 +820,37 @@ export const ControlPlaneSourcesLive = HttpApiBuilder.group( · 121 unmodified lines | |||
| 687 | error: urlParams.error, | 820 | error: urlParams.error, |
| 688 | errorDescription: urlParams.error_description, | 821 | errorDescription: urlParams.error_description, |
| 689 | }).pipe( | 822 | }).pipe( |
| 690 | Effect.map((source) => `Source connected: ${source.id}. You can close this window.`), | 823 | Effect.map((source) => |
| 824 | htmlResponse( | ||
| 825 | sourceOAuthPopupResultDocument({ | ||
| 826 | title: "Source connected", | ||
| 827 | message: `Source connected: ${source.id}. You can close this window.`, | ||
| 828 | state: "stored", | ||
| 829 | payload: { | ||
| 830 | type: "executor-v3:source-oauth-result", | ||
| 831 | ok: true, | ||
| 832 | sourceId: source.id, | ||
| 833 | }, | ||
| 834 | }), | ||
| 835 | ), | ||
| 836 | ), | ||
| 837 | Effect.catchAll((error) => | ||
| 838 | Effect.succeed( | ||
| 839 | htmlResponse( | ||
| 840 | sourceOAuthPopupResultDocument({ | ||
| 841 | title: "OAuth failed", | ||
| 842 | message: error instanceof Error ? error.message : "Failed completing OAuth", | ||
| 843 | state: "cancelled", | ||
| 844 | payload: { | ||
| 845 | type: "executor-v3:source-oauth-result", | ||
| 846 | ok: false, | ||
| 847 | error: error instanceof Error ? error.message : "Failed completing OAuth", | ||
| 848 | }, | ||
| 849 | }), | ||
| 850 | 500, | ||
| 851 | ), | ||
| 852 | ), | ||
| 853 | ), | ||
| 691 | ), | 854 | ), |
| 692 | ), | 855 | ), |
| 693 | ); | 856 | ); |
| @@ -461,6 +461,22 @@ export type ExecutorAddSourceInput = · 460 unmodified lines | |||
| 461 | auth?: ExecutorOpenApiSourceAuthInput | null; | 461 | auth?: ExecutorOpenApiSourceAuthInput | null; |
| 462 | }; | 462 | }; |
| 463 | 463 | ||
| 464 | export type ConnectMcpSourceInput = { | ||
| 465 | workspaceId: WorkspaceId; | ||
| 466 | sourceId?: Source["id"] | null; | ||
| 467 | endpoint: string; | ||
| 468 | name?: string | null; | ||
| 469 | namespace?: string | null; | ||
| 470 | enabled?: boolean; | ||
| 471 | transport?: Source["transport"]; | ||
| 472 | queryParams?: Source["queryParams"]; | ||
| 473 | headers?: Source["headers"]; | ||
| 474 | }; | ||
| 475 | |||
| 476 | export type McpSourceConnectResult = Extract<ExecutorSourceAddResult, { | ||
| 477 | kind: "connected" | "oauth_required"; | ||
| 478 | }>; | ||
| 479 | |||
| 464 | export const shouldPromptForOpenApiCredentialSetup = (input: { | 480 | export const shouldPromptForOpenApiCredentialSetup = (input: { |
| 465 | existing?: Source; | 481 | existing?: Source; |
| 466 | auth?: ExecutorOpenApiSourceAuthInput | null; | 482 | auth?: ExecutorOpenApiSourceAuthInput | null; |
| @@ -539,6 +555,208 @@ const materializeExecutorOpenApiAuth = (input: { · 72 unmodified lines | |||
| 539 | } satisfies Source["auth"]; | 555 | } satisfies Source["auth"]; |
| 540 | }); | 556 | }); |
| 541 | 557 | ||
| 558 | const connectMcpSourceInternal = (input: { | ||
| 559 | rows: SqlControlPlaneRows; | ||
| 560 | getLocalServerBaseUrl?: () => string | undefined; | ||
| 561 | workspaceId: WorkspaceId; | ||
| 562 | sourceId?: Source["id"] | null; | ||
| 563 | executionId?: SourceAuthSession["executionId"]; | ||
| 564 | interactionId?: SourceAuthSession["interactionId"]; | ||
| 565 | endpoint: string; | ||
| 566 | name?: string | null; | ||
| 567 | namespace?: string | null; | ||
| 568 | enabled?: boolean; | ||
| 569 | transport?: Source["transport"]; | ||
| 570 | queryParams?: Source["queryParams"]; | ||
| 571 | headers?: Source["headers"]; | ||
| 572 | mcpDiscoveryElicitation?: McpDiscoveryElicitationContext; | ||
| 573 | resolveSecretMaterial: ResolveSecretMaterial; | ||
| 574 | }): Effect.Effect<McpSourceConnectResult, Error, never> => | ||
| 575 | Effect.gen(function* () { | ||
| 576 | const normalizedEndpoint = normalizeEndpoint(input.endpoint); | ||
| 577 | const existing = yield* ( | ||
| 578 | input.sourceId | ||
| 579 | ? loadSourceById(input.rows, { | ||
| 580 | workspaceId: input.workspaceId, | ||
| 581 | sourceId: input.sourceId, | ||
| 582 | }).pipe( | ||
| 583 | Effect.flatMap((source) => | ||
| 584 | source.kind === "mcp" | ||
| 585 | ? Effect.succeed(source) | ||
| 586 | : Effect.fail(new Error(`Expected MCP source, received ${source.kind}`)), | ||
| 587 | ), | ||
| 588 | ) | ||
| 589 | : loadSourcesInWorkspace(input.rows, input.workspaceId).pipe( | ||
| 590 | Effect.map((sources) => | ||
| 591 | sources.find( | ||
| 592 | (source) => | ||
| 593 | source.kind === "mcp" && normalizeEndpoint(source.endpoint) === normalizedEndpoint, | ||
| 594 | ), | ||
| 595 | ), | ||
| 596 | ) | ||
| 597 | ); | ||
| 598 | |||
| 599 | const chosenName = | ||
| 600 | trimOrNull(input.name) ?? existing?.name ?? defaultSourceNameFromEndpoint(normalizedEndpoint); | ||
| 601 | const chosenNamespace = | ||
| 602 | trimOrNull(input.namespace) | ||
| 603 | ?? existing?.namespace | ||
| 604 | ?? defaultNamespaceFromName(chosenName); | ||
| 605 | const chosenEnabled = input.enabled ?? existing?.enabled ?? true; | ||
| 606 | const chosenTransport = input.transport ?? existing?.transport ?? "auto"; | ||
| 607 | const chosenQueryParams = | ||
| 608 | input.queryParams !== undefined ? input.queryParams : (existing?.queryParams ?? null); | ||
| 609 | const chosenHeaders = | ||
| 610 | input.headers !== undefined ? input.headers : (existing?.headers ?? null); | ||
| 611 | const now = Date.now(); | ||
| 612 | |||
| 613 | const draftSource = existing | ||
| 614 | ? yield* updateSourceFromPayload({ | ||
| 615 | source: existing, | ||
| 616 | payload: { | ||
| 617 | name: chosenName, | ||
| 618 | endpoint: normalizedEndpoint, | ||
| 619 | namespace: chosenNamespace, | ||
| 620 | kind: "mcp", | ||
| 621 | status: "probing", | ||
| 622 | enabled: chosenEnabled, | ||
| 623 | transport: chosenTransport, | ||
| 624 | queryParams: chosenQueryParams, | ||
| 625 | headers: chosenHeaders, | ||
| 626 | auth: { kind: "none" }, | ||
| 627 | lastError: null, | ||
| 628 | }, | ||
| 629 | now, | ||
| 630 | }) | ||
| 631 | : yield* createSourceFromPayload({ | ||
| 632 | workspaceId: input.workspaceId, | ||
| 633 | sourceId: SourceIdSchema.make(`src_${crypto.randomUUID()}`), | ||
| 634 | payload: { | ||
| 635 | name: chosenName, | ||
| 636 | kind: "mcp", | ||
| 637 | endpoint: normalizedEndpoint, | ||
| 638 | namespace: chosenNamespace, | ||
| 639 | status: "probing", | ||
| 640 | enabled: chosenEnabled, | ||
| 641 | transport: chosenTransport, | ||
| 642 | queryParams: chosenQueryParams, | ||
| 643 | headers: chosenHeaders, | ||
| 644 | auth: { kind: "none" }, | ||
| 645 | }, | ||
| 646 | now, | ||
| 647 | }); | ||
| 648 | |||
| 649 | const persistedDraft = yield* persistSource(input.rows, draftSource); | ||
| 650 | yield* syncSourceToolArtifacts({ | ||
| 651 | rows: input.rows, | ||
| 652 | source: persistedDraft, | ||
| 653 | resolveSecretMaterial: input.resolveSecretMaterial, | ||
| 654 | }); | ||
| 655 | |||
| 656 | const discovered = yield* Effect.either( | ||
| 657 | probeMcpSourceWithoutAuth( | ||
| 658 | persistedDraft, | ||
| 659 | input.mcpDiscoveryElicitation, | ||
| 660 | ), | ||
| 661 | ); | ||
| 662 | |||
| 663 | const connectedResult = yield* Either.match(discovered, { | ||
| 664 | onLeft: () => Effect.succeed(null), | ||
| 665 | onRight: (result) => | ||
| 666 | Effect.gen(function* () { | ||
| 667 | const connected = yield* updateSourceStatus(input.rows, persistedDraft, { | ||
| 668 | status: "connected", | ||
| 669 | lastError: null, | ||
| 670 | auth: { kind: "none" }, | ||
| 671 | }); | ||
| 672 | const indexed = yield* Effect.either( | ||
| 673 | persistMcpToolArtifactsFromManifest({ | ||
| 674 | rows: input.rows, | ||
| 675 | source: connected, | ||
| 676 | manifestEntries: result.manifest.tools, | ||
| 677 | }), | ||
| 678 | ); | ||
| 679 | |||
| 680 | return yield* Either.match(indexed, { | ||
| 681 | onLeft: (error) => | ||
| 682 | updateSourceStatus(input.rows, connected, { | ||
| 683 | status: "error", | ||
| 684 | lastError: error.message, | ||
| 685 | }).pipe( | ||
| 686 | Effect.zipRight(Effect.fail(error)), | ||
| 687 | ), | ||
| 688 | onRight: () => | ||
| 689 | Effect.succeed({ | ||
| 690 | kind: "connected", | ||
| 691 | source: connected, | ||
| 692 | } satisfies McpSourceConnectResult), | ||
| 693 | }); | ||
| 694 | }), | ||
| 695 | }); | ||
| 696 | |||
| 697 | if (connectedResult) { | ||
| 698 | return connectedResult; | ||
| 699 | } | ||
| 700 | |||
| 701 | const localServerBaseUrl = input.getLocalServerBaseUrl?.(); | ||
| 702 | if (!localServerBaseUrl) { | ||
| 703 | return yield* Effect.fail( | ||
| 704 | new Error("Local executor server base URL is unavailable for source credential setup"), | ||
| 705 | ); | ||
| 706 | } | ||
| 707 | |||
| 708 | const sessionId = SourceAuthSessionIdSchema.make(`src_auth_${crypto.randomUUID()}`); | ||
| 709 | const state = crypto.randomUUID(); | ||
| 710 | const redirectUrl = resolveSourceCredentialOauthCompleteUrl({ | ||
| 711 | baseUrl: localServerBaseUrl, | ||
| 712 | workspaceId: input.workspaceId, | ||
| 713 | sourceId: persistedDraft.id, | ||
| 714 | }); | ||
| 715 | const oauthStart = yield* startOAuthAuthorization({ | ||
| 716 | endpoint: normalizedEndpoint, | ||
| 717 | redirectUrl, | ||
| 718 | state, | ||
| 719 | }); | ||
| 720 | |||
| 721 | const authRequiredSource = yield* updateSourceStatus(input.rows, persistedDraft, { | ||
| 722 | status: "auth_required", | ||
| 723 | lastError: null, | ||
| 724 | }); | ||
| 725 | |||
| 726 | const sessionNow = Date.now(); | ||
| 727 | yield* input.rows.sourceAuthSessions.upsert({ | ||
| 728 | id: sessionId, | ||
| 729 | workspaceId: input.workspaceId, | ||
| 730 | sourceId: authRequiredSource.id, | ||
| 731 | executionId: input.executionId ?? null, | ||
| 732 | interactionId: input.interactionId ?? null, | ||
| 733 | strategy: "oauth2_authorization_code", | ||
| 734 | status: "pending", | ||
| 735 | endpoint: normalizedEndpoint, | ||
| 736 | state, | ||
| 737 | redirectUri: redirectUrl, | ||
| 738 | scope: null, | ||
| 739 | resourceMetadataUrl: oauthStart.resourceMetadataUrl, | ||
| 740 | authorizationServerUrl: oauthStart.authorizationServerUrl, | ||
| 741 | resourceMetadataJson: oauthStart.resourceMetadataJson, | ||
| 742 | authorizationServerMetadataJson: oauthStart.authorizationServerMetadataJson, | ||
| 743 | clientInformationJson: oauthStart.clientInformationJson, | ||
| 744 | codeVerifier: oauthStart.codeVerifier, | ||
| 745 | authorizationUrl: oauthStart.authorizationUrl, | ||
| 746 | errorText: null, | ||
| 747 | completedAt: null, | ||
| 748 | createdAt: sessionNow, | ||
| 749 | updatedAt: sessionNow, | ||
| 750 | }); | ||
| 751 | |||
| 752 | return { | ||
| 753 | kind: "oauth_required", | ||
| 754 | source: authRequiredSource, | ||
| 755 | sessionId, | ||
| 756 | authorizationUrl: oauthStart.authorizationUrl, | ||
| 757 | } satisfies McpSourceConnectResult; | ||
| 758 | }); | ||
| 759 | |||
| 542 | type RuntimeSourceAuthServiceShape = { | 760 | type RuntimeSourceAuthServiceShape = { |
| 543 | getSourceById: (input: { | 761 | getSourceById: (input: { |
| 544 | workspaceId: WorkspaceId; | 762 | workspaceId: WorkspaceId; |
| @@ -555,6 +773,9 @@ type RuntimeSourceAuthServiceShape = { · 10 unmodified lines | |||
| 555 | mcpDiscoveryElicitation?: McpDiscoveryElicitationContext; | 773 | mcpDiscoveryElicitation?: McpDiscoveryElicitationContext; |
| 556 | }, | 774 | }, |
| 557 | ) => Effect.Effect<ExecutorSourceAddResult, Error, never>; | 775 | ) => Effect.Effect<ExecutorSourceAddResult, Error, never>; |
| 776 | connectMcpSource: ( | ||
| 777 | input: ConnectMcpSourceInput, | ||
| 778 | ) => Effect.Effect<McpSourceConnectResult, Error, never>; | ||
| 558 | completeSourceCredentialSetup: (input: { | 779 | completeSourceCredentialSetup: (input: { |
| 559 | workspaceId: WorkspaceId; | 780 | workspaceId: WorkspaceId; |
| 560 | sourceId: Source["id"]; | 781 | sourceId: Source["id"]; |
| @@ -733,169 +954,37 @@ export const createRuntimeSourceAuthService = (input: { · 172 unmodified lines | |||
| 733 | ), | 954 | ), |
| 734 | }); | 955 | }); |
| 735 | }) | 956 | }) |
| 736 | : Effect.gen(function* () { | 957 | : connectMcpSourceInternal({ |
| 737 | const normalizedEndpoint = normalizeEndpoint(sourceInput.endpoint); | 958 | rows: input.rows, |
| 738 | const existingSources = yield* loadSourcesInWorkspace( | 959 | getLocalServerBaseUrl: input.getLocalServerBaseUrl, |
| 739 | input.rows, | 960 | workspaceId: sourceInput.workspaceId, |
| 740 | sourceInput.workspaceId, | 961 | executionId: sourceInput.executionId, |
| 741 | ); | 962 | interactionId: sourceInput.interactionId, |
| 742 | const existing = existingSources.find( | 963 | endpoint: sourceInput.endpoint, |
| 743 | (source) => source.kind === "mcp" && normalizeEndpoint(source.endpoint) === normalizedEndpoint, | 964 | name: sourceInput.name, |
| 744 | ); | 965 | namespace: sourceInput.namespace, |
| 745 | 966 | mcpDiscoveryElicitation: options?.mcpDiscoveryElicitation, | |
| 746 | const chosenName = | 967 | resolveSecretMaterial, |
| 747 | trimOrNull(sourceInput.name) | ||
| 748 | ?? existing?.name | ||
| 749 | ?? defaultSourceNameFromEndpoint(normalizedEndpoint); | ||
| 750 | const chosenNamespace = | ||
| 751 | trimOrNull(sourceInput.namespace) | ||
| 752 | ?? existing?.namespace | ||
| 753 | ?? defaultNamespaceFromName(chosenName); | ||
| 754 | const now = Date.now(); | ||
| 755 | |||
| 756 | const draftSource = existing | ||
| 757 | ? yield* updateSourceFromPayload({ | ||
| 758 | source: existing, | ||
| 759 | payload: { | ||
| 760 | name: chosenName, | ||
| 761 | endpoint: normalizedEndpoint, | ||
| 762 | namespace: chosenNamespace, | ||
| 763 | kind: "mcp", | ||
| 764 | status: "probing", | ||
| 765 | enabled: true, | ||
| 766 | transport: existing.transport ?? "auto", | ||
| 767 | auth: { kind: "none" }, | ||
| 768 | lastError: null, | ||
| 769 | }, | ||
| 770 | now, | ||
| 771 | }) | ||
| 772 | : yield* createSourceFromPayload({ | ||
| 773 | workspaceId: sourceInput.workspaceId, | ||
| 774 | sourceId: SourceIdSchema.make(`src_${crypto.randomUUID()}`), | ||
| 775 | payload: { | ||
| 776 | name: chosenName, | ||
| 777 | kind: "mcp", | ||
| 778 | endpoint: normalizedEndpoint, | ||
| 779 | namespace: chosenNamespace, | ||
| 780 | status: "probing", | ||
| 781 | enabled: true, | ||
| 782 | transport: "auto", | ||
| 783 | auth: { kind: "none" }, | ||
| 784 | }, | ||
| 785 | now, | ||
| 786 | }); | ||
| 787 | |||
| 788 | const persistedDraft = yield* persistSource(input.rows, draftSource); | ||
| 789 | yield* syncSourceToolArtifacts({ | ||
| 790 | rows: input.rows, | ||
| 791 | source: persistedDraft, | ||
| 792 | resolveSecretMaterial, | ||
| 793 | }); | ||
| 794 | |||
| 795 | const discovered = yield* Effect.either( | ||
| 796 | probeMcpSourceWithoutAuth( | ||
| 797 | persistedDraft, | ||
| 798 | options?.mcpDiscoveryElicitation, | ||
| 799 | ), | ||
| 800 | ); | ||
| 801 | |||
| 802 | const connectedResult = yield* Either.match(discovered, { | ||
| 803 | onLeft: () => Effect.succeed(null), | ||
| 804 | onRight: (result) => | ||
| 805 | Effect.gen(function* () { | ||
| 806 | const connected = yield* updateSourceStatus(input.rows, persistedDraft, { | ||
| 807 | status: "connected", | ||
| 808 | lastError: null, | ||
| 809 | auth: { kind: "none" }, | ||
| 810 | }); | ||
| 811 | const indexed = yield* Effect.either( | ||
| 812 | persistMcpToolArtifactsFromManifest({ | ||
| 813 | rows: input.rows, | ||
| 814 | source: connected, | ||
| 815 | manifestEntries: result.manifest.tools, | ||
| 816 | }), | ||
| 817 | ); | ||
| 818 | |||
| 819 | return yield* Either.match(indexed, { | ||
| 820 | onLeft: (error) => | ||
| 821 | updateSourceStatus(input.rows, connected, { | ||
| 822 | status: "error", | ||
| 823 | lastError: error.message, | ||
| 824 | }).pipe( | ||
| 825 | Effect.zipRight(Effect.fail(error)), | ||
| 826 | ), | ||
| 827 | onRight: () => | ||
| 828 | Effect.succeed({ | ||
| 829 | kind: "connected", | ||
| 830 | source: connected, | ||
| 831 | } satisfies ExecutorSourceAddResult), | ||
| 832 | }); | ||
| 833 | }), | ||
| 834 | }); | ||
| 835 | |||
| 836 | if (connectedResult) { | ||
| 837 | return connectedResult; | ||
| 838 | } | ||
| 839 | |||
| 840 | const localServerBaseUrl = input.getLocalServerBaseUrl?.(); | ||
| 841 | if (!localServerBaseUrl) { | ||
| 842 | return yield* Effect.fail( | ||
| 843 | new Error("Local executor server base URL is unavailable for source credential setup"), | ||
| 844 | ); | ||
| 845 | } | ||
| 846 | |||
| 847 | const sessionId = SourceAuthSessionIdSchema.make(`src_auth_${crypto.randomUUID()}`); | ||
| 848 | const state = crypto.randomUUID(); | ||
| 849 | const redirectUrl = resolveSourceCredentialOauthCompleteUrl({ | ||
| 850 | baseUrl: localServerBaseUrl, | ||
| 851 | workspaceId: sourceInput.workspaceId, | ||
| 852 | sourceId: persistedDraft.id, | ||
| 853 | }); | ||
| 854 | const oauthStart = yield* startOAuthAuthorization({ | ||
| 855 | endpoint: normalizedEndpoint, | ||
| 856 | redirectUrl, | ||
| 857 | state, | ||
| 858 | }); | ||
| 859 | |||
| 860 | const authRequiredSource = yield* updateSourceStatus(input.rows, persistedDraft, { | ||
| 861 | status: "auth_required", | ||
| 862 | lastError: null, | ||
| 863 | }); | ||
| 864 | |||
| 865 | const sessionNow = Date.now(); | ||
| 866 | yield* input.rows.sourceAuthSessions.upsert({ | ||
| 867 | id: sessionId, | ||
| 868 | workspaceId: sourceInput.workspaceId, | ||
| 869 | sourceId: authRequiredSource.id, | ||
| 870 | executionId: sourceInput.executionId, | ||
| 871 | interactionId: sourceInput.interactionId, | ||
| 872 | strategy: "oauth2_authorization_code", | ||
| 873 | status: "pending", | ||
| 874 | endpoint: normalizedEndpoint, | ||
| 875 | state, | ||
| 876 | redirectUri: redirectUrl, | ||
| 877 | scope: null, | ||
| 878 | resourceMetadataUrl: oauthStart.resourceMetadataUrl, | ||
| 879 | authorizationServerUrl: oauthStart.authorizationServerUrl, | ||
| 880 | resourceMetadataJson: oauthStart.resourceMetadataJson, | ||
| 881 | authorizationServerMetadataJson: oauthStart.authorizationServerMetadataJson, | ||
| 882 | clientInformationJson: oauthStart.clientInformationJson, | ||
| 883 | codeVerifier: oauthStart.codeVerifier, | ||
| 884 | authorizationUrl: oauthStart.authorizationUrl, | ||
| 885 | errorText: null, | ||
| 886 | completedAt: null, | ||
| 887 | createdAt: sessionNow, | ||
| 888 | updatedAt: sessionNow, | ||
| 889 | }); | ||
| 890 | |||
| 891 | return { | ||
| 892 | kind: "oauth_required", | ||
| 893 | source: authRequiredSource, | ||
| 894 | sessionId, | ||
| 895 | authorizationUrl: oauthStart.authorizationUrl, | ||
| 896 | } satisfies ExecutorSourceAddResult; | ||
| 897 | }), | 968 | }), |
| 898 | 969 | ||
| 970 | connectMcpSource: (sourceInput) => | ||
| 971 | connectMcpSourceInternal({ | ||
| 972 | rows: input.rows, | ||
| 973 | getLocalServerBaseUrl: input.getLocalServerBaseUrl, | ||
| 974 | workspaceId: sourceInput.workspaceId, | ||
| 975 | sourceId: sourceInput.sourceId, | ||
| 976 | executionId: null, | ||
| 977 | interactionId: null, | ||
| 978 | endpoint: sourceInput.endpoint, | ||
| 979 | name: sourceInput.name, | ||
| 980 | namespace: sourceInput.namespace, | ||
| 981 | enabled: sourceInput.enabled, | ||
| 982 | transport: sourceInput.transport, | ||
| 983 | queryParams: sourceInput.queryParams, | ||
| 984 | headers: sourceInput.headers, | ||
| 985 | resolveSecretMaterial, | ||
| 986 | }), | ||
| 987 | |||
| 899 | completeSourceCredentialSetup: ({ | 988 | completeSourceCredentialSetup: ({ |
| 900 | workspaceId, | 989 | workspaceId, |
| 901 | sourceId, | 990 | sourceId, |
| @@ -1,4 +1,5 @@ | |||
| 1 | import type { | 1 | import type { |
| 2 | ConnectMcpSourcePayload, | ||
| 2 | CreateSourcePayload, | 3 | CreateSourcePayload, |
| 3 | UpdateSourcePayload, | 4 | UpdateSourcePayload, |
| 4 | } from "../api/sources/api"; | 5 | } from "../api/sources/api"; |
| @@ -23,6 +24,7 @@ import { · 18 unmodified lines | |||
| 23 | operationErrors, | 24 | operationErrors, |
| 24 | } from "./operation-errors"; | 25 | } from "./operation-errors"; |
| 25 | import { createDefaultSecretMaterialResolver } from "./secret-material-providers"; | 26 | import { createDefaultSecretMaterialResolver } from "./secret-material-providers"; |
| 27 | import { RuntimeSourceAuthServiceTag } from "./source-auth-service"; | ||
| 26 | import { ControlPlaneStore, type ControlPlaneStoreShape } from "./store"; | 28 | import { ControlPlaneStore, type ControlPlaneStoreShape } from "./store"; |
| 27 | import { syncSourceToolArtifacts } from "./tool-artifacts"; | 29 | import { syncSourceToolArtifacts } from "./tool-artifacts"; |
| 28 | import { | 30 | import { |
| @@ -37,6 +39,7 @@ const sourceOps = { · 8 unmodified lines | |||
| 37 | create: operationErrors("sources.create"), | 39 | create: operationErrors("sources.create"), |
| 38 | get: operationErrors("sources.get"), | 40 | get: operationErrors("sources.get"), |
| 39 | update: operationErrors("sources.update"), | 41 | update: operationErrors("sources.update"), |
| 42 | connectMcp: operationErrors("sources.connectMcp"), | ||
| 40 | remove: operationErrors("sources.remove"), | 43 | remove: operationErrors("sources.remove"), |
| 41 | } as const; | 44 | } as const; |
| 42 | 45 | ||
| @@ -210,6 +213,40 @@ export const updateSource = (input: { · 167 unmodified lines | |||
| 210 | }); | 213 | }); |
| 211 | })); | 214 | })); |
| 212 | 215 | ||
| 216 | export const connectMcpSource = (input: { | ||
| 217 | workspaceId: WorkspaceId; | ||
| 218 | payload: ConnectMcpSourcePayload; | ||
| 219 | }) => | ||
| 220 | Effect.gen(function* () { | ||
| 221 | const sourceAuthService = yield* RuntimeSourceAuthServiceTag; | ||
| 222 | |||
| 223 | return yield* sourceAuthService.connectMcpSource({ | ||
| 224 | workspaceId: input.workspaceId, | ||
| 225 | sourceId: input.payload.sourceId, | ||
| 226 | endpoint: input.payload.endpoint, | ||
| 227 | name: input.payload.name, | ||
| 228 | namespace: input.payload.namespace, | ||
| 229 | enabled: input.payload.enabled, | ||
| 230 | transport: input.payload.transport, | ||
| 231 | queryParams: input.payload.queryParams, | ||
| 232 | headers: input.payload.headers, | ||
| 233 | }).pipe( | ||
| 234 | Effect.mapError((cause) => { | ||
| 235 | if (cause instanceof Error && cause.message.startsWith("Source not found:")) { | ||
| 236 | return sourceOps.connectMcp.notFound( | ||
| 237 | "Source not found", | ||
| 238 | `workspaceId=${input.workspaceId} sourceId=${input.payload.sourceId}`, | ||
| 239 | ); | ||
| 240 | } | ||
| 241 | |||
| 242 | return sourceOps.connectMcp.unknownStorage( | ||
| 243 | cause, | ||
| 244 | "Failed preparing MCP source OAuth flow", | ||
| 245 | ); | ||
| 246 | }), | ||
| 247 | ); | ||
| 248 | }); | ||
| 249 | |||
| 213 | export const removeSource = (input: { | 250 | export const removeSource = (input: { |
| 214 | workspaceId: WorkspaceId; | 251 | workspaceId: WorkspaceId; |
| 215 | sourceId: SourceId; | 252 | sourceId: SourceId; |