Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

fix(ruvocal): keep env MCP server credentials when the browser echoes the server back (#2901)

kevin93275a4a82587885
diff --git a/ruflo/src/ruvocal/src/lib/server/textGeneration/mcp/mergeServers.spec.ts b/ruflo/src/ruvocal/src/lib/server/textGeneration/mcp/mergeServers.spec.ts
new file mode 100644
index 0000000000..7499e01001
--- /dev/null
+++ b/ruflo/src/ruvocal/src/lib/server/textGeneration/mcp/mergeServers.spec.ts
@@ -0,0 +1,54 @@
+import { describe, it, expect } from "vitest";
+import { mergeMcpServers } from "./mergeServers";
+
+const base = [
+ {
+ name: "GitHub",
+ url: "https://api.githubcopilot.com/mcp/readonly",
+ headers: { Authorization: "Bearer server-token" },
+ },
+ { name: "Docs", url: "https://docs.example/mcp" },
+];
+
+describe("mergeMcpServers", () => {
+ it("keeps a base server's credentials when the browser echoes it back without headers", () => {
+ const merged = mergeMcpServers(base, [
+ { name: "GitHub", url: "https://api.githubcopilot.com/mcp/readonly" },
+ ]);
+ expect(merged.find((s) => s.name === "GitHub")?.headers).toEqual({
+ Authorization: "Bearer server-token",
+ });
+ });
+
+ it("treats an empty headers object like no headers", () => {
+ const merged = mergeMcpServers(base, [
+ { name: "GitHub", url: "https://api.githubcopilot.com/mcp/readonly", headers: {} },
+ ]);
+ expect(merged.find((s) => s.name === "GitHub")?.headers?.Authorization).toBe(
+ "Bearer server-token"
+ );
+ });
+
+ it("lets request-supplied headers win", () => {
+ const merged = mergeMcpServers(base, [
+ {
+ name: "GitHub",
+ url: "https://api.githubcopilot.com/mcp/readonly",
+ headers: { Authorization: "Bearer user-token" },
+ },
+ ]);
+ expect(merged.find((s) => s.name === "GitHub")?.headers).toEqual({
+ Authorization: "Bearer user-token",
+ });
+ });
+
+ it("never sends a base server's credentials to a different URL", () => {
+ const merged = mergeMcpServers(base, [{ name: "GitHub", url: "https://evil.example/mcp" }]);
+ expect(merged.find((s) => s.name === "GitHub")?.headers).toBeUndefined();
+ });
+
+ it("adds new request-only servers and keeps unrelated base servers", () => {
+ const merged = mergeMcpServers(base, [{ name: "Mine", url: "https://mine.example/mcp" }]);
+ expect(merged.map((s) => s.name)).toEqual(["GitHub", "Docs", "Mine"]);
+ });
+});
diff --git a/ruflo/src/ruvocal/src/lib/server/textGeneration/mcp/mergeServers.ts b/ruflo/src/ruvocal/src/lib/server/textGeneration/mcp/mergeServers.ts
new file mode 100644
index 0000000000..d8eb520b72
--- /dev/null
+++ b/ruflo/src/ruvocal/src/lib/server/textGeneration/mcp/mergeServers.ts
@@ -0,0 +1,31 @@
+export type McpServerEntry = { name: string; url: string; headers?: Record<string, string> };
+
+/**
+ * Merge request-provided MCP servers over the env-configured base servers, by name.
+ *
+ * The request wins, but `/api/mcp/servers` deliberately withholds base-server headers
+ * from the browser, so the copy the client echoes back has none. Replacing the base
+ * entry wholesale would drop its server-side credentials (e.g. `Authorization`), and
+ * every tool listing from the chat UI would then fail with 401 (#2901).
+ *
+ * So a request entry that carries no headers of its own keeps the base entry's
+ * headers, but only when it points at the same URL: a request must not be able to
+ * redirect a server-side token to another host by reusing a base server's name.
+ */
+export function mergeMcpServers(
+ base: McpServerEntry[],
+ custom: McpServerEntry[]
+): McpServerEntry[] {
+ const byName = new Map<string, McpServerEntry>();
+ for (const s of base) byName.set(s.name, s);
+ for (const s of custom) {
+ const existing = byName.get(s.name);
+ const hasOwnHeaders = !!s.headers && Object.keys(s.headers).length > 0;
+ if (!hasOwnHeaders && existing?.headers && existing.url === s.url) {
+ byName.set(s.name, { ...s, headers: existing.headers });
+ } else {
+ byName.set(s.name, s);
+ }
+ }
+ return [...byName.values()];
+}
diff --git a/ruflo/src/ruvocal/src/lib/server/textGeneration/mcp/runMcpFlow.ts b/ruflo/src/ruvocal/src/lib/server/textGeneration/mcp/runMcpFlow.ts
index 067fb2de34..f32d64bce6 100644
--- a/ruflo/src/ruvocal/src/lib/server/textGeneration/mcp/runMcpFlow.ts
+++ b/ruflo/src/ruvocal/src/lib/server/textGeneration/mcp/runMcpFlow.ts
@@ -1,4 +1,5 @@
import { config } from "$lib/server/config";
+import { mergeMcpServers } from "./mergeServers";
import { MessageUpdateType, type MessageUpdate } from "$lib/types/MessageUpdate";
import { getMcpServers } from "$lib/server/mcp/registry";
import { isValidUrl } from "$lib/server/urlSafety";
@@ -98,13 +99,7 @@ export async function* runMcpFlow({
// Invalidate cached tool list when the set of servers changes at request-time
resetMcpToolsCache();
// Deduplicate by server name (request takes precedence)
- const byName = new Map<
- string,
- { name: string; url: string; headers?: Record<string, string> }
- >();
- for (const s of servers) byName.set(s.name, s);
- for (const s of custom) byName.set(s.name, s);
- servers = [...byName.values()];
+ servers = mergeMcpServers(servers, custom);
try {
logger.debug(
{