Repository navigation
fix(mcp): proxy standalone tools to running server when reachable #161
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
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| const DEFAULT_URL = "http://localhost:3111"; | ||
| const HEALTH_PROBE_TIMEOUT_MS = 500; | ||
| const CALL_TIMEOUT_MS = 15_000; | ||
| const LOCAL_MODE_TTL_MS = 30_000; | ||
|
|
||
| export interface ProxyHandle { | ||
| mode: "proxy"; | ||
| baseUrl: string; | ||
| call: (path: string, init?: RequestInit) => Promise<unknown>; | ||
| } | ||
|
|
||
| export interface LocalHandle { | ||
| mode: "local"; | ||
| } | ||
|
|
||
| export type Handle = ProxyHandle | LocalHandle; | ||
|
|
||
| let cached: Handle | null = null; | ||
| let cachedAt = 0; | ||
| let probeInFlight: Promise<Handle> | null = null; | ||
|
|
||
| function baseUrl(): string { | ||
| return (process.env["AGENTMEMORY_URL"] || DEFAULT_URL).replace(/\/+$/, ""); | ||
| } | ||
|
|
||
| function authHeader(): Record<string, string> { | ||
| const secret = process.env["AGENTMEMORY_SECRET"]; | ||
| return secret ? { authorization: `Bearer ${secret}` } : {}; | ||
| } | ||
|
|
||
| async function probe(url: string): Promise<boolean> { | ||
| try { | ||
| const res = await fetch(`${url}/agentmemory/livez`, { | ||
| method: "GET", | ||
| headers: authHeader(), | ||
| signal: AbortSignal.timeout(HEALTH_PROBE_TIMEOUT_MS), | ||
| }); | ||
| return res.ok; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| export function invalidateHandle(): void { | ||
| cached = null; | ||
| cachedAt = 0; | ||
| } | ||
|
|
||
| export async function resolveHandle(): Promise<Handle> { | ||
| const now = Date.now(); | ||
| if (cached) { | ||
| if (cached.mode === "local" && now - cachedAt >= LOCAL_MODE_TTL_MS) { | ||
| cached = null; | ||
| cachedAt = 0; | ||
| } else { | ||
| return cached; | ||
| } | ||
| } | ||
| if (probeInFlight) return probeInFlight; | ||
| const url = baseUrl(); | ||
| probeInFlight = (async () => { | ||
| const up = await probe(url); | ||
| if (up) { | ||
| const handle: ProxyHandle = { | ||
| mode: "proxy", | ||
| baseUrl: url, | ||
| call: async (path, init) => { | ||
| const res = await fetch(`${url}${path}`, { | ||
| ...init, | ||
| headers: { | ||
| "content-type": "application/json", | ||
| ...authHeader(), | ||
| ...(init?.headers as Record<string, string> | undefined), | ||
| }, | ||
| signal: AbortSignal.timeout(CALL_TIMEOUT_MS), | ||
| }); | ||
| if (!res.ok) { | ||
| throw new Error( | ||
| `${init?.method || "GET"} ${path} -> ${res.status} ${res.statusText}`, | ||
| ); | ||
| } | ||
| const text = await res.text(); | ||
| return text ? JSON.parse(text) : null; | ||
| }, | ||
| }; | ||
| cached = handle; | ||
| cachedAt = Date.now(); | ||
| return handle; | ||
| } | ||
| const local: LocalHandle = { mode: "local" }; | ||
| cached = local; | ||
| cachedAt = Date.now(); | ||
| return local; | ||
| })(); | ||
| try { | ||
| return await probeInFlight; | ||
| } finally { | ||
| probeInFlight = null; | ||
| } | ||
| } | ||
|
|
||
| export function resetHandleForTests(): void { | ||
| cached = null; | ||
| cachedAt = 0; | ||
| probeInFlight = null; | ||
| } | ||
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.
🧩 Analysis chain
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 210
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 1547
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 113
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 88
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 2970
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 173
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 4788
🏁 Script executed:
Repository: rohitg00/agentmemory
Length of output: 915
Resolved handle is cached permanently — no recovery from startup-order or server bouncing.
Once the first probe resolves, the cache is never invalidated. If the server was down at MCP startup but brought up later, MCP stays on local KV forever (writes never reach the server). If the server goes down after being probed as up, every call still hits proxy first, fails, and silently falls back to local; once it's back up, there's no re-sync and any memories written during the outage live only in local KV.
This is the split-data scenario issue
#159addresses. Consider invalidatingcachedwhen a proxy call throws (so the nextresolveHandle()re-probes), and/or adding a short TTL on the local-mode cache so a later-started server is eventually discovered.♻️ Sketch: invalidate on proxy failure + short local-mode TTL
Then call
invalidateHandle()fromhandleToolCall's proxy-failure catch instandalone.ts.🤖 Prompt for AI Agents