Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix: use internal URL for MCP tool requests (#1556)

Florian Meyer7d9d9d212d13
diff --git a/.env.sample b/.env.sample
index fddd65b85..d83e6364b 100644
--- a/.env.sample
+++ b/.env.sample
@@ -1,6 +1,7 @@
# General URLs
KANEO_CLIENT_URL=http://localhost:5173
# KANEO_API_URL=http://localhost:1337 # optional - defaults to KANEO_CLIENT_URL/api
+# KANEO_INTERNAL_API_URL=http://127.0.0.1:1337 # optional - internal MCP tool requests only
# CORS_ORIGINS= # optional - comma-separated; defaults to KANEO_CLIENT_URL
# Database
diff --git a/ENVIRONMENT_SETUP.md b/ENVIRONMENT_SETUP.md
index 67af859af..1a6380e19 100644
--- a/ENVIRONMENT_SETUP.md
+++ b/ENVIRONMENT_SETUP.md
@@ -43,6 +43,7 @@ For local development, the web app also supports:
### Optional Variables
Kaneo supports many optional configuration options including:
+- `KANEO_INTERNAL_API_URL` - API origin used only for server-side requests from the built-in HTTP MCP endpoint. Defaults to `http://127.0.0.1:1337`; override it only if the API is not reachable there from its own process.
- SSO providers (GitHub OAuth via `GITHUB_OAUTH_CLIENT_ID` / `GITHUB_OAUTH_CLIENT_SECRET`, Google, Discord, Custom OAuth/OIDC)
- GitHub repository integration (GitHub App: `GITHUB_APP_ID`, `GITHUB_PRIVATE_KEY`, `GITHUB_WEBHOOK_SECRET`, optional `GITHUB_APP_NAME`), separate from GitHub SSO
- SMTP configuration for email
diff --git a/apps/api/src/mcp/index.ts b/apps/api/src/mcp/index.ts
index b11d0ed3e..897be3aa6 100644
--- a/apps/api/src/mcp/index.ts
+++ b/apps/api/src/mcp/index.ts
@@ -28,10 +28,14 @@ import {
} from "./schemas";
import { registerMcpTools, toMcpToolRegistrar } from "./tools";
-const apiUrl = (process.env.KANEO_API_URL || "http://localhost:1337").replace(
- /\/api\/?$/,
- "",
-);
+const publicApiUrl = (process.env.KANEO_API_URL || "http://localhost:1337")
+ .replace(/\/api\/?$/, "")
+ .replace(/\/+$/, "");
+const internalApiUrl = (
+ process.env.KANEO_INTERNAL_API_URL || "http://127.0.0.1:1337"
+)
+ .replace(/\/api\/?$/, "")
+ .replace(/\/+$/, "");
type McpSession = {
transport: WebStandardStreamableHTTPServerTransport;
@@ -45,7 +49,7 @@ function createMcpServerForUser(token: string): LegacyMcpServer {
name: "kaneo-mcp",
version: "1.0.0",
});
- registerMcpTools(toMcpToolRegistrar(server), apiUrl, token);
+ registerMcpTools(toMcpToolRegistrar(server), internalApiUrl, token);
return server;
}
@@ -248,17 +252,17 @@ mcp.post("/mcp/token", async (c) => {
mcp.get("/.well-known/oauth-protected-resource/api/mcp", (c) =>
c.json({
- resource: `${apiUrl}/api/mcp`,
- authorization_servers: [`${apiUrl}/api`],
+ resource: `${publicApiUrl}/api/mcp`,
+ authorization_servers: [`${publicApiUrl}/api`],
}),
);
mcp.get("/.well-known/oauth-authorization-server/api", (c) =>
c.json({
- issuer: `${apiUrl}/api`,
- authorization_endpoint: `${apiUrl}/api/mcp/authorize`,
- token_endpoint: `${apiUrl}/api/mcp/token`,
- registration_endpoint: `${apiUrl}/api/mcp/register`,
+ issuer: `${publicApiUrl}/api`,
+ authorization_endpoint: `${publicApiUrl}/api/mcp/authorize`,
+ token_endpoint: `${publicApiUrl}/api/mcp/token`,
+ registration_endpoint: `${publicApiUrl}/api/mcp/register`,
response_types_supported: ["code"],
grant_types_supported: ["authorization_code"],
code_challenge_methods_supported: ["S256"],
@@ -269,7 +273,7 @@ mcp.get("/.well-known/oauth-authorization-server/api", (c) =>
mcp.all("/mcp", async (c) => {
const authResult = await validateBearerToken(c.req.raw);
if (!authResult) {
- const prmUrl = `${apiUrl}/api/.well-known/oauth-protected-resource/api/mcp`;
+ const prmUrl = `${publicApiUrl}/api/.well-known/oauth-protected-resource/api/mcp`;
c.header("WWW-Authenticate", `Bearer resource_metadata="${prmUrl}"`);
return c.json(
{
@@ -301,7 +305,7 @@ mcp.all("/mcp", async (c) => {
}
if (!(await isLegacyRequest(c.req.raw.clone()))) {
- const modern = createModernMcpHandler(authResult.token, apiUrl);
+ const modern = createModernMcpHandler(authResult.token, internalApiUrl);
return modern.fetch(c.req.raw);
}
diff --git a/apps/docs/core/installation/environment-variables.mdx b/apps/docs/core/installation/environment-variables.mdx
index 2f8daa53e..cda625bc2 100644
--- a/apps/docs/core/installation/environment-variables.mdx
+++ b/apps/docs/core/installation/environment-variables.mdx
@@ -13,6 +13,7 @@ The variables are split up into the following sections:
- [General URLs](#general-urls)
- [Database](#database)
- [Authentication](#authentication)
+- [MCP](#mcp)
- [Object Storage](#object-storage)
- [Access Control](#access-control)
- [GitHub SSO](#github-sso)
@@ -57,6 +58,12 @@ Name | Description |
## Optional variables
+### MCP
+
+Name | Description | Default |
+--- | --- | --- |
+| `KANEO_INTERNAL_API_URL` | API origin used only for server-side requests from the built-in HTTP MCP endpoint. Override it only if the API is not reachable at the default loopback address from its own process. | `http://127.0.0.1:1337` |
+
### Object Storage
Kaneo uses S3-compatible object storage for private uploads in task descriptions and task comments.
diff --git a/tests/api/mcp-internal-api-url.test.ts b/tests/api/mcp-internal-api-url.test.ts
new file mode 100644
index 000000000..5ee656945
--- /dev/null
+++ b/tests/api/mcp-internal-api-url.test.ts
@@ -0,0 +1,106 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+const authMocks = vi.hoisted(() => ({
+ getSession: vi.fn(async () => ({ user: { id: "test-user" } })),
+}));
+
+vi.mock("../../apps/api/src/auth", () => ({
+ auth: { api: { getSession: authMocks.getSession } },
+}));
+
+const protocolVersion = "2026-07-28";
+
+function toolRequest() {
+ return new Request("http://public.test:5273/mcp", {
+ method: "POST",
+ headers: {
+ accept: "application/json, text/event-stream",
+ authorization: "Bearer test-token",
+ "content-type": "application/json",
+ "mcp-method": "tools/call",
+ "mcp-name": "whoami",
+ "mcp-protocol-version": protocolVersion,
+ },
+ body: JSON.stringify({
+ jsonrpc: "2.0",
+ id: 1,
+ method: "tools/call",
+ params: {
+ name: "whoami",
+ arguments: {},
+ _meta: {
+ "io.modelcontextprotocol/protocolVersion": protocolVersion,
+ "io.modelcontextprotocol/clientInfo": {
+ name: "kaneo-internal-url-test",
+ version: "1.0.0",
+ },
+ "io.modelcontextprotocol/clientCapabilities": {},
+ },
+ },
+ }),
+ });
+}
+
+async function loadMcpRoutes(internalApiUrl?: string) {
+ vi.stubEnv("KANEO_API_URL", "http://public.test:5273/api");
+ vi.stubEnv("KANEO_INTERNAL_API_URL", internalApiUrl);
+ vi.resetModules();
+ return (await import("../../apps/api/src/mcp")).default;
+}
+
+afterEach(() => {
+ vi.unstubAllGlobals();
+ vi.unstubAllEnvs();
+ vi.resetModules();
+ authMocks.getSession.mockClear();
+});
+
+describe("MCP API URLs", () => {
+ it("advertises the public URL while fetching tools through the internal URL", async () => {
+ const apiFetch = vi.fn(async () =>
+ Response.json({ user: { id: "test-user" } }),
+ );
+ vi.stubGlobal("fetch", apiFetch);
+ const mcpRoutes = await loadMcpRoutes();
+
+ const metadataResponse = await mcpRoutes.request(
+ "/.well-known/oauth-authorization-server/api",
+ );
+ const metadata = (await metadataResponse.json()) as {
+ issuer: string;
+ authorization_endpoint: string;
+ };
+
+ expect(metadata).toMatchObject({
+ issuer: "http://public.test:5273/api",
+ authorization_endpoint: "http://public.test:5273/api/mcp/authorize",
+ });
+
+ const toolResponse = await mcpRoutes.request(toolRequest());
+
+ expect(toolResponse.status).toBe(200);
+ expect(apiFetch).toHaveBeenCalledOnce();
+ expect(String(apiFetch.mock.calls[0]?.[0])).toBe(
+ "http://127.0.0.1:1337/api/auth/get-session",
+ );
+ });
+
+ it.each(["http://api.internal:1337/api/", "http://api.internal:1337/"])(
+ "uses the configured internal URL %s without duplicate slashes",
+ async (internalApiUrl) => {
+ const apiFetch = vi.fn(async () =>
+ Response.json({ user: { id: "test-user" } }),
+ );
+ vi.stubGlobal("fetch", apiFetch);
+ const mcpRoutes = await loadMcpRoutes(internalApiUrl);
+
+ const toolResponse = await mcpRoutes.request(toolRequest());
+
+ expect(toolResponse.status).toBe(200);
+ expect(apiFetch).toHaveBeenCalledOnce();
+ expect(String(apiFetch.mock.calls[0]?.[0])).toBe(
+ "http://api.internal:1337/api/auth/get-session",
+ );
+ },
+ );
+});