Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ jobs:
SRH_TOKEN: example_token
SRH_CONNECTION_STRING: "redis://redis:6379"
env:
MCP_ROUTER_SERVER_URL: https://router.mcprouter.app/sse
MCP_ROUTER_SERVER_URL: https://router.mcprouter.app/stream
MCP_ROUTER_SERVER_API_KEY: ${{ secrets.MCP_ROUTER_SERVER_API_KEY }}
QSTASH_TOKEN: eyJVc2VySUQiOiJkZWZhdWx0VXNlciIsIlBhc3N3b3JkIjoiZGVmYXVsdFBhc3N3b3JkIn0
QSTASH_URL: http://localhost:8080
Expand Down
24 changes: 13 additions & 11 deletions lib/ai/mcp.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { experimental_createMCPClient as createMCPClientSDK } from 'ai';

export const createMCPClient = () =>
createMCPClientSDK({
transport: {
type: 'sse',
// biome-ignore lint/style/noNonNullAssertion: <explanation>
url: process.env.MCP_ROUTER_SERVER_URL!,
headers: {
// biome-ignore lint/style/noNonNullAssertion: <explanation>
'x-api-key': process.env.MCP_ROUTER_SERVER_API_KEY!,
},
},
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';

export const createMCPClient = () => {
// biome-ignore lint/style/noNonNullAssertion: <explanation>
const url = new URL(process.env.MCP_ROUTER_SERVER_URL!);
// add x-api-key to the url
// biome-ignore lint/style/noNonNullAssertion: <explanation>
url.searchParams.set('x-api-key', process.env.MCP_ROUTER_SERVER_API_KEY!);
Comment on lines +7 to +10
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Security concern: API key exposed in URL query parameters.

Adding the API key as a URL query parameter poses security risks as query parameters can be logged in server logs, browser history, and proxy logs. Consider using headers or request body instead.

-  const url = new URL(process.env.MCP_ROUTER_SERVER_URL!);
-  // add x-api-key to the url
-  // biome-ignore lint/style/noNonNullAssertion: <explanation>
-  url.searchParams.set('x-api-key', process.env.MCP_ROUTER_SERVER_API_KEY!);
+  const url = new URL(process.env.MCP_ROUTER_SERVER_URL!);

Then pass the API key via headers in the transport options:

-  const transport = new StreamableHTTPClientTransport(url, {});
+  const transport = new StreamableHTTPClientTransport(url, {
+    headers: {
+      'x-api-key': process.env.MCP_ROUTER_SERVER_API_KEY!
+    }
+  });
🤖 Prompt for AI Agents
In lib/ai/mcp.ts around lines 7 to 10, the API key is currently added as a URL
query parameter, which exposes it to logs and browser history. To fix this,
remove the API key from the URL query parameters and instead include it in the
request headers when making the HTTP call. Update the code to set the
'x-api-key' header in the transport or fetch options rather than appending it to
the URL.


const transport = new StreamableHTTPClientTransport(url, {});
return createMCPClientSDK({
transport,
});
};
Comment on lines +5 to +16
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling and environment variable validation.

The function uses non-null assertions without validating required environment variables, which could lead to runtime errors.

 export const createMCPClient = () => {
+  const serverUrl = process.env.MCP_ROUTER_SERVER_URL;
+  const apiKey = process.env.MCP_ROUTER_SERVER_API_KEY;
+  
+  if (!serverUrl) {
+    throw new Error('MCP_ROUTER_SERVER_URL environment variable is required');
+  }
+  
+  if (!apiKey) {
+    throw new Error('MCP_ROUTER_SERVER_API_KEY environment variable is required');
+  }
+  
+  let url: URL;
+  try {
+    url = new URL(serverUrl);
+  } catch (error) {
+    throw new Error(`Invalid MCP_ROUTER_SERVER_URL: ${error.message}`);
+  }
+
-  // biome-ignore lint/style/noNonNullAssertion: <explanation>
-  const url = new URL(process.env.MCP_ROUTER_SERVER_URL!);
-  // add x-api-key to the url
-  // biome-ignore lint/style/noNonNullAssertion: <explanation>
-  url.searchParams.set('x-api-key', process.env.MCP_ROUTER_SERVER_API_KEY!);

-  const transport = new StreamableHTTPClientTransport(url, {});
+  const transport = new StreamableHTTPClientTransport(url, {
+    headers: {
+      'x-api-key': apiKey
+    }
+  });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export const createMCPClient = () => {
// biome-ignore lint/style/noNonNullAssertion: <explanation>
const url = new URL(process.env.MCP_ROUTER_SERVER_URL!);
// add x-api-key to the url
// biome-ignore lint/style/noNonNullAssertion: <explanation>
url.searchParams.set('x-api-key', process.env.MCP_ROUTER_SERVER_API_KEY!);
const transport = new StreamableHTTPClientTransport(url, {});
return createMCPClientSDK({
transport,
});
};
export const createMCPClient = () => {
const serverUrl = process.env.MCP_ROUTER_SERVER_URL;
const apiKey = process.env.MCP_ROUTER_SERVER_API_KEY;
if (!serverUrl) {
throw new Error('MCP_ROUTER_SERVER_URL environment variable is required');
}
if (!apiKey) {
throw new Error('MCP_ROUTER_SERVER_API_KEY environment variable is required');
}
let url: URL;
try {
url = new URL(serverUrl);
} catch (error) {
throw new Error(`Invalid MCP_ROUTER_SERVER_URL: ${error.message}`);
}
const transport = new StreamableHTTPClientTransport(url, {
headers: {
'x-api-key': apiKey
}
});
return createMCPClientSDK({
transport,
});
};
🤖 Prompt for AI Agents
In lib/ai/mcp.ts around lines 5 to 16, the function createMCPClient uses
non-null assertions on environment variables without validation, risking runtime
errors. Add checks to verify that MCP_ROUTER_SERVER_URL and
MCP_ROUTER_SERVER_API_KEY are defined before using them. If either is missing,
throw a clear error to prevent proceeding with invalid configuration. This
ensures safer and more predictable behavior.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@faker-js/faker": "^9.8.0",
"@langchain/textsplitters": "^0.1.0",
"@lexical/react": "^0.32.1",
"@modelcontextprotocol/sdk": "^1.13.2",
"@monaco-editor/react": "^4.7.0",
"@openrouter/ai-sdk-provider": "0.7.2",
"@opentelemetry/api": "^1.9.0",
Expand Down
19 changes: 11 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading