-
Notifications
You must be signed in to change notification settings - Fork 51
feat(github): per-repo import connections with scoped OAuth tokens #3484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Local GitHub MCP (wrangler dev in ../mcps/github) | ||
| # Both /mcp and /api/mcp work on the local Worker. | ||
| VITE_GITHUB_MCP_URL=http://localhost:8787/api/mcp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { | ||
| encodeMeshOAuthClientState, | ||
| getGithubConnectionRepoScope, | ||
| githubConnectionTitle, | ||
| isGithubMcpConnection, | ||
| } from "./github-connection"; | ||
| import { | ||
| isGithubMcpConnectionUrl, | ||
| isLocalGithubMcpUrl, | ||
| } from "./github-mcp-url"; | ||
|
|
||
| describe("github-connection", () => { | ||
| test("isGithubMcpConnection matches app_name and canonical URL", () => { | ||
| expect(isGithubMcpConnection({ app_name: "mcp-github" })).toBe(true); | ||
| expect( | ||
| isGithubMcpConnection({ | ||
| connection_url: "https://github-mcp.decocms.com/mcp", | ||
| }), | ||
| ).toBe(true); | ||
| expect( | ||
| isGithubMcpConnection({ | ||
| connection_url: "http://localhost:8787/api/mcp", | ||
| }), | ||
| ).toBe(true); | ||
| expect(isGithubMcpConnection({ app_name: "other" })).toBe(false); | ||
| }); | ||
|
|
||
| test("getGithubConnectionRepoScope reads repositoryId from metadata", () => { | ||
| expect( | ||
| getGithubConnectionRepoScope({ | ||
| githubRepo: { | ||
| owner: "deco", | ||
| name: "mesh", | ||
| url: "https://github.com/deco/mesh", | ||
| repositoryId: 123, | ||
| installationId: 456, | ||
| }, | ||
| }), | ||
| ).toEqual({ | ||
| owner: "deco", | ||
| name: "mesh", | ||
| url: "https://github.com/deco/mesh", | ||
| repositoryId: 123, | ||
| installationId: 456, | ||
| }); | ||
| }); | ||
|
|
||
| test("encodeMeshOAuthClientState round-trips repositoryId", () => { | ||
| const encoded = encodeMeshOAuthClientState({ repositoryId: 99 }); | ||
| expect(encoded.startsWith("mesh:")).toBe(true); | ||
| }); | ||
|
|
||
| test("githubConnectionTitle formats owner/repo", () => { | ||
| expect(githubConnectionTitle("deco", "mesh")).toBe("GitHub — deco/mesh"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("github-mcp-url", () => { | ||
| test("isLocalGithubMcpUrl detects localhost", () => { | ||
| expect(isLocalGithubMcpUrl("http://localhost:8787/api/mcp")).toBe(true); | ||
| expect(isLocalGithubMcpUrl("https://github-mcp.decocms.com/mcp")).toBe( | ||
| false, | ||
| ); | ||
| }); | ||
|
|
||
| test("isGithubMcpConnectionUrl accepts local and prod hosts", () => { | ||
| expect(isGithubMcpConnectionUrl("http://localhost:8787/mcp")).toBe(true); | ||
| expect(isGithubMcpConnectionUrl("http://localhost:8787/api/mcp")).toBe( | ||
| true, | ||
| ); | ||
| expect(isGithubMcpConnectionUrl("https://github-mcp.decocms.com/mcp")).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /** | ||
| * GitHub MCP connection helpers — repo-scoped tokens and connection detection. | ||
| */ | ||
|
|
||
| import { isGithubMcpConnectionUrl } from "./github-mcp-url"; | ||
|
|
||
| export const GITHUB_MCP_APP_NAME = "mcp-github"; | ||
| export const GITHUB_MCP_HOST = "api.githubcopilot.com"; | ||
| export const GITHUB_MCP_PROXY_HOST = "github-mcp.decocms.com"; | ||
|
|
||
| export interface GithubConnectionRepoScope { | ||
| owner: string; | ||
| name: string; | ||
| url: string; | ||
| repositoryId: number; | ||
| installationId?: number; | ||
| } | ||
|
|
||
| export function isGithubMcpConnection(connection: { | ||
| app_name?: string | null; | ||
| connection_url?: string | null; | ||
| }): boolean { | ||
| if (connection.app_name === GITHUB_MCP_APP_NAME) return true; | ||
| const url = connection.connection_url; | ||
| if (typeof url !== "string" || url.length === 0) return false; | ||
| return isGithubMcpConnectionUrl(url); | ||
| } | ||
|
|
||
| export function getGithubConnectionRepoScope( | ||
| metadata: Record<string, unknown> | null | undefined, | ||
| ): GithubConnectionRepoScope | null { | ||
| const githubRepo = metadata?.githubRepo; | ||
| if (!githubRepo || typeof githubRepo !== "object") return null; | ||
|
|
||
| const record = githubRepo as Record<string, unknown>; | ||
| const owner = record.owner; | ||
| const name = record.name; | ||
| const url = record.url; | ||
| const repositoryId = record.repositoryId; | ||
|
|
||
| if ( | ||
| typeof owner !== "string" || | ||
| typeof name !== "string" || | ||
| typeof url !== "string" || | ||
| typeof repositoryId !== "number" | ||
| ) { | ||
| return null; | ||
| } | ||
|
|
||
| const installationId = record.installationId; | ||
| return { | ||
| owner, | ||
| name, | ||
| url, | ||
| repositoryId, | ||
| installationId: | ||
| typeof installationId === "number" ? installationId : undefined, | ||
| }; | ||
| } | ||
|
|
||
| export function githubConnectionTitle(owner: string, name: string): string { | ||
| return `GitHub — ${owner}/${name}`; | ||
| } | ||
|
|
||
| export function encodeMeshOAuthClientState(state: { | ||
| repositoryId?: number; | ||
| }): string { | ||
| const json = JSON.stringify(state); | ||
| const base64 = btoa(json) | ||
| .replace(/\+/g, "-") | ||
| .replace(/\//g, "_") | ||
| .replace(/=+$/, ""); | ||
| return `mesh:${base64}`; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /** | ||
| * Dev override for the GitHub MCP connection URL. | ||
| * | ||
| * In development, Studio points new GitHub imports at a local Worker | ||
| * (wrangler dev) instead of github-mcp.decocms.com. Set | ||
| * VITE_GITHUB_MCP_URL in apps/mesh/.env.development to change the target. | ||
| */ | ||
|
|
||
| const DEFAULT_LOCAL_GITHUB_MCP_URL = "http://localhost:8787/api/mcp"; | ||
|
|
||
| function readViteEnv(name: string): string | undefined { | ||
| const value = (import.meta.env as Record<string, string | undefined>)[name]; | ||
| return typeof value === "string" && value.length > 0 ? value : undefined; | ||
| } | ||
|
|
||
| /** Resolve the HTTP URL used when creating a GitHub MCP connection. */ | ||
| export function resolveGithubMcpConnectionUrl( | ||
| registryUrl: string | undefined, | ||
| ): string { | ||
| const override = readViteEnv("VITE_GITHUB_MCP_URL"); | ||
| if (override) return override; | ||
|
|
||
| if (import.meta.env.DEV) { | ||
| return DEFAULT_LOCAL_GITHUB_MCP_URL; | ||
| } | ||
|
|
||
| if (!registryUrl) { | ||
| throw new Error("Registry item is missing a remote URL for mcp-github"); | ||
| } | ||
|
|
||
| return registryUrl; | ||
| } | ||
|
|
||
| export function isLocalGithubMcpUrl(url: string): boolean { | ||
| try { | ||
| const parsed = new URL(url); | ||
| return ( | ||
| parsed.hostname === "localhost" || | ||
| parsed.hostname === "127.0.0.1" || | ||
| parsed.hostname.endsWith(".localhost") | ||
| ); | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| export function isGithubMcpConnectionUrl(url: string): boolean { | ||
| try { | ||
| const parsed = new URL(url); | ||
| const path = parsed.pathname.replace(/\/+$/, ""); | ||
| if (!path.endsWith("/mcp")) return false; | ||
|
|
||
| return ( | ||
| parsed.hostname === "github-mcp.decocms.com" || | ||
| parsed.hostname === "api.githubcopilot.com" || | ||
| isLocalGithubMcpUrl(url) | ||
| ); | ||
| } catch { | ||
| return false; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ import { | |||||||||||||||||||||||
| refreshAndStore, | ||||||||||||||||||||||||
| } from "@/oauth/token-refresh"; | ||||||||||||||||||||||||
| import { DownstreamTokenStorage } from "../../storage/downstream-token"; | ||||||||||||||||||||||||
| import { getGithubConnectionRepoScope } from "@/shared/github-connection"; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const GITHUB_API = "https://api.github.com"; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
@@ -40,6 +41,22 @@ export const GITHUB_LIST_USER_ORGS = defineTool({ | |||||||||||||||||||||||
| await ctx.access.check(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const tokenStorage = new DownstreamTokenStorage(ctx.db, ctx.vault); | ||||||||||||||||||||||||
| const organizationId = ctx.organization?.id; | ||||||||||||||||||||||||
| if (!organizationId) { | ||||||||||||||||||||||||
| throw new Error("Organization context required"); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const connection = await ctx.storage.connections.findById( | ||||||||||||||||||||||||
| input.connectionId, | ||||||||||||||||||||||||
| organizationId, | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
|
Comment on lines
+49
to
+52
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Validate that the org-scoped connection exists before continuing. Without this check, the tool can still operate using a token row keyed only by Prompt for AI agents
Suggested change
|
||||||||||||||||||||||||
| const repoScope = getGithubConnectionRepoScope( | ||||||||||||||||||||||||
| (connection?.metadata ?? null) as Record<string, unknown> | null, | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| const refreshOptions = repoScope | ||||||||||||||||||||||||
| ? { repositoryId: repoScope.repositoryId } | ||||||||||||||||||||||||
| : undefined; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let token = await tokenStorage.get(input.connectionId); | ||||||||||||||||||||||||
| if (!token) { | ||||||||||||||||||||||||
| throw new Error( | ||||||||||||||||||||||||
|
|
@@ -55,7 +72,11 @@ export const GITHUB_LIST_USER_ORGS = defineTool({ | |||||||||||||||||||||||
| canRefresh(token) && | ||||||||||||||||||||||||
| tokenStorage.isExpired(token, PROACTIVE_REFRESH_BUFFER_MS) | ||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||
| const refreshed = await refreshAndStore(token, tokenStorage); | ||||||||||||||||||||||||
| const refreshed = await refreshAndStore( | ||||||||||||||||||||||||
| token, | ||||||||||||||||||||||||
| tokenStorage, | ||||||||||||||||||||||||
| refreshOptions, | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| if (!refreshed) { | ||||||||||||||||||||||||
| throw new Error(RECONNECT_ERROR); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
@@ -101,7 +122,11 @@ export const GITHUB_LIST_USER_ORGS = defineTool({ | |||||||||||||||||||||||
| if (!current || !canRefresh(current)) { | ||||||||||||||||||||||||
| throw new Error(RECONNECT_ERROR); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| const refreshed = await refreshAndStore(current, tokenStorage); | ||||||||||||||||||||||||
| const refreshed = await refreshAndStore( | ||||||||||||||||||||||||
| current, | ||||||||||||||||||||||||
| tokenStorage, | ||||||||||||||||||||||||
| refreshOptions, | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| if (!refreshed) { | ||||||||||||||||||||||||
| throw new Error(RECONNECT_ERROR); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Validate that the requested
owner/namematches the connection’s scoped repo before usingrepositoryIdfor refresh, otherwise token scope and clone target can diverge.Prompt for AI agents