-
Notifications
You must be signed in to change notification settings - Fork 6
refactor: Use HTTP transport for MCP to properly pass authInfo #139
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
Merged
sweetmantech
merged 8 commits into
test
from
sweetmantech/myc-3980-api-pass-authinfo-to-the-mcp-server
Jan 20, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7b4247c
refactor: Use HTTP transport for MCP to properly pass authInfo
sweetmantech 297aaca
fix: Make authToken optional for internal flows
sweetmantech e793993
refactor: Extract getBaseUrl to lib/networking
sweetmantech c94fc04
refactor: Extract getMcpTools to lib/mcp
sweetmantech 6ce9df7
test: Add unit tests for getBaseUrl and getMcpTools
sweetmantech 5ea3ff4
refactor: Use AI SDK built-in SSE transport for MCP
sweetmantech e6663c1
fix: Correct MCP endpoint URL to /mcp
sweetmantech 076e86c
fix: Use HTTP transport instead of SSE for MCP
sweetmantech 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
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,63 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
|
|
||
| vi.mock("@ai-sdk/mcp", () => ({ | ||
| experimental_createMCPClient: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/networking/getBaseUrl", () => ({ | ||
| getBaseUrl: vi.fn().mockReturnValue("https://test.vercel.app"), | ||
| })); | ||
|
|
||
| import { getMcpTools } from "../getMcpTools"; | ||
| import { experimental_createMCPClient } from "@ai-sdk/mcp"; | ||
|
|
||
| const mockCreateMCPClient = vi.mocked(experimental_createMCPClient); | ||
|
|
||
| describe("getMcpTools", () => { | ||
| const mockTools = { | ||
| tool1: { description: "Tool 1", parameters: {} }, | ||
| tool2: { description: "Tool 2", parameters: {} }, | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
|
|
||
| mockCreateMCPClient.mockResolvedValue({ | ||
| tools: vi.fn().mockResolvedValue(mockTools), | ||
| } as any); | ||
| }); | ||
|
|
||
| it("creates MCP client with HTTP transport config", async () => { | ||
| await getMcpTools("test-token"); | ||
|
|
||
| expect(mockCreateMCPClient).toHaveBeenCalledWith({ | ||
| transport: { | ||
| type: "http", | ||
| url: "https://test.vercel.app/mcp", | ||
| headers: { | ||
| Authorization: "Bearer test-token", | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it("returns tools from MCP client", async () => { | ||
| const result = await getMcpTools("test-token"); | ||
|
|
||
| expect(result).toEqual(mockTools); | ||
| }); | ||
|
|
||
| it("passes different auth tokens correctly", async () => { | ||
| await getMcpTools("different-token"); | ||
|
|
||
| expect(mockCreateMCPClient).toHaveBeenCalledWith({ | ||
| transport: { | ||
| type: "http", | ||
| url: "https://test.vercel.app/mcp", | ||
| headers: { | ||
| Authorization: "Bearer different-token", | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
| }); |
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,23 @@ | ||
| import { ToolSet } from "ai"; | ||
| import { experimental_createMCPClient as createMCPClient } from "@ai-sdk/mcp"; | ||
| import { getBaseUrl } from "@/lib/networking/getBaseUrl"; | ||
|
|
||
| /** | ||
| * Fetches MCP tools via HTTP transport with authentication. | ||
| * | ||
| * @param authToken - The auth token to use for MCP endpoint authentication | ||
| * @returns The MCP tools as a ToolSet | ||
| */ | ||
| export async function getMcpTools(authToken: string): Promise<ToolSet> { | ||
| const mcpClient = await createMCPClient({ | ||
| transport: { | ||
| type: "http", | ||
| url: `${getBaseUrl()}/mcp`, | ||
| headers: { | ||
| Authorization: `Bearer ${authToken}`, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| return (await mcpClient.tools()) as ToolSet; | ||
| } |
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,39 @@ | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; | ||
| import { getBaseUrl } from "../getBaseUrl"; | ||
|
|
||
| describe("getBaseUrl", () => { | ||
| const originalEnv = process.env; | ||
|
|
||
| beforeEach(() => { | ||
| vi.resetModules(); | ||
| process.env = { ...originalEnv }; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| process.env = originalEnv; | ||
| }); | ||
|
|
||
| it("returns HTTPS URL when VERCEL_URL is set", () => { | ||
| process.env.VERCEL_URL = "my-app.vercel.app"; | ||
|
|
||
| const result = getBaseUrl(); | ||
|
|
||
| expect(result).toBe("https://my-app.vercel.app"); | ||
| }); | ||
|
|
||
| it("returns localhost when VERCEL_URL is not set", () => { | ||
| delete process.env.VERCEL_URL; | ||
|
|
||
| const result = getBaseUrl(); | ||
|
|
||
| expect(result).toBe("http://localhost:3000"); | ||
| }); | ||
|
|
||
| it("returns localhost when VERCEL_URL is empty string", () => { | ||
| process.env.VERCEL_URL = ""; | ||
|
|
||
| const result = getBaseUrl(); | ||
|
|
||
| expect(result).toBe("http://localhost:3000"); | ||
| }); | ||
| }); |
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.
Documentation comment references incorrect MCP endpoint path /api/mcp instead of /mcp
View Details
📝 Patch Details
Analysis
Bug Explanation:
The comment at line 10 of
lib/chat/setupToolsForRequest.tscontains an inaccurate reference to the MCP endpoint. The comment states "via HTTP transport to /api/mcp for proper auth", but the actual endpoint is/mcp, not/api/mcp.This was confirmed by examining:
app/mcp/route.ts(notapp/api/mcp/route.ts), which in Next.js app router maps to the/mcpendpointlib/mcp/getMcpTools.tsat line 14 constructs the URL as${getBaseUrl()}/mcp, confirming the endpoint is/mcpWhile this is a documentation comment and doesn't affect runtime behavior, it creates confusion for developers reading the code who might think the endpoint should be at
/api/mcp.Fix Applied:
Changed line 10 from:
to:
This ensures the comment accurately reflects the actual endpoint being used throughout the codebase.