Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(mcp-host): verify scoped session agents

Josuée6a2b2b7c686
diff --git a/mcp-host/src/core/conversation/__tests__/sessionKeyParts.test.ts b/mcp-host/src/core/conversation/__tests__/sessionKeyParts.test.ts
new file mode 100644
index 000000000..1292d6db7
--- /dev/null
+++ b/mcp-host/src/core/conversation/__tests__/sessionKeyParts.test.ts
@@ -0,0 +1,27 @@
+import { describe, expect, it } from 'vitest'
+import { sessionPartsFromPrefixedKey } from '../sessionKeyParts'
+
+describe('sessionPartsFromPrefixedKey', () => {
+ it('preserves scoped chat ids when the explicit agent matches the prefix', () => {
+ expect(
+ sessionPartsFromPrefixedKey(
+ 'subject:rpc:embedded:rpc:rpc:chat:trailing:',
+ 'subject:rpc:embedded:rpc:rpc:',
+ 'rpc'
+ )
+ ).toEqual({ agent: 'rpc', chatId: 'chat:trailing:' })
+ })
+
+ it('rejects an explicit scoped agent that does not match the prefix', () => {
+ expect(
+ sessionPartsFromPrefixedKey('u-1:rpc:agent-a:chat-1', 'u-1:rpc:agent-a:', 'agent-b')
+ ).toBeNull()
+ })
+
+ it('preserves trailing colons in unscoped chat ids', () => {
+ expect(sessionPartsFromPrefixedKey('u-1:rpc:agent-a:chat:', 'u-1:rpc:')).toEqual({
+ agent: 'agent-a',
+ chatId: 'chat:',
+ })
+ })
+})
diff --git a/mcp-host/src/core/conversation/sessionKeyParts.ts b/mcp-host/src/core/conversation/sessionKeyParts.ts
index 182f93009..108593284 100644
--- a/mcp-host/src/core/conversation/sessionKeyParts.ts
+++ b/mcp-host/src/core/conversation/sessionKeyParts.ts
@@ -20,7 +20,10 @@ export function sessionPartsFromPrefixedKey(
const rest = key.slice(prefix.length)
if (!rest) return null
- if (scopedAgent) return { agent: scopedAgent, chatId: rest }
+ if (scopedAgent) {
+ if (!prefix.endsWith(`:rpc:${scopedAgent}:`)) return null
+ return { agent: scopedAgent, chatId: rest }
+ }
const colonIndex = rest.indexOf(':')
if (colonIndex <= 0 || colonIndex === rest.length - 1) return null