agentbay_agent_memory_query
Query agent memory across projects to retrieve patterns, decisions, and insights. Access your own memory or read another agent's memory if permitted, using tags, search, and type filters.
Instructions
Query your own agent memory, or read another agent's memory (if they granted you access). Agent memory persists across all projects.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| type | No | ||
| tags | No | ||
| search | No | Search query | |
| source | No | ||
| limit | No | ||
| targetAgentId | No | Query another agent's memory (requires read permission) |
Implementation Reference
- src/index.ts:417-453 (registration)Tool registration of 'agentbay_agent_memory_query' via server.tool() with name, description, Zod schema for inputs, and async handler function.
// Tool 19: Agent Memory Query server.tool( 'agentbay_agent_memory_query', 'Query your own agent memory, or read another agent\'s memory (if they granted you access). Agent memory persists across all projects.', { type: z.enum(['PATTERN', 'PITFALL', 'ARCHITECTURE', 'DEPENDENCY', 'TEST_INSIGHT', 'PERFORMANCE', 'DECISION', 'CONTEXT']).optional(), tags: z.array(z.string()).optional(), search: z.string().optional().describe('Search query'), source: z.string().optional(), limit: z.number().min(1).max(100).optional(), targetAgentId: z.string().optional().describe('Query another agent\'s memory (requires read permission)'), }, async ({ type, tags, search, source, limit, targetAgentId }) => { let agentId = targetAgentId; if (!agentId) { const meData = await apiGet('/api/v1/me'); agentId = meData.agentId; if (!agentId) return { content: [{ type: 'text' as const, text: 'Error: No agent linked to this API key. Use agentbay_agent_register first.' }] }; } const params = new URLSearchParams(); if (type) params.set('type', type); if (tags?.length) params.set('tags', tags.join(',')); if (search) params.set('search', search); if (source) params.set('source', source); if (limit) params.set('limit', String(limit)); const data = await apiGet(`/api/v1/agents/${agentId}/memory?${params.toString()}`); if (data.error) return { content: [{ type: 'text' as const, text: `Error: ${data.error}` }] }; const entries = data.entries || data.memories || []; if (!entries.length) return { content: [{ type: 'text' as const, text: 'No agent memory entries found. Note: agent_memory_query browses by type/tags. Use "search" parameter for text search.' }] }; const text = entries.map((k: any) => `### ${k.title} (${k.type})\nID: ${k.id}\n${k.content}\n_Source: ${k.source || 'unknown'} | Confidence: ${k.confidence} | Tags: ${k.tags?.join(', ') || 'none'}_` ).join('\n\n'); return { content: [{ type: 'text' as const, text: `Agent memory (${entries.length}):\n\n${text}` }] }; } ); - src/index.ts:421-428 (schema)Input schema (Zod) for agent_memory_query: optional type, tags, search, source, limit, and targetAgentId parameters.
{ type: z.enum(['PATTERN', 'PITFALL', 'ARCHITECTURE', 'DEPENDENCY', 'TEST_INSIGHT', 'PERFORMANCE', 'DECISION', 'CONTEXT']).optional(), tags: z.array(z.string()).optional(), search: z.string().optional().describe('Search query'), source: z.string().optional(), limit: z.number().min(1).max(100).optional(), targetAgentId: z.string().optional().describe('Query another agent\'s memory (requires read permission)'), }, - src/index.ts:429-453 (handler)Handler function: resolves agent ID (from targetAgentId or /api/v1/me), builds query params, calls GET /api/v1/agents/{agentId}/memory, formats and returns results.
async ({ type, tags, search, source, limit, targetAgentId }) => { let agentId = targetAgentId; if (!agentId) { const meData = await apiGet('/api/v1/me'); agentId = meData.agentId; if (!agentId) return { content: [{ type: 'text' as const, text: 'Error: No agent linked to this API key. Use agentbay_agent_register first.' }] }; } const params = new URLSearchParams(); if (type) params.set('type', type); if (tags?.length) params.set('tags', tags.join(',')); if (search) params.set('search', search); if (source) params.set('source', source); if (limit) params.set('limit', String(limit)); const data = await apiGet(`/api/v1/agents/${agentId}/memory?${params.toString()}`); if (data.error) return { content: [{ type: 'text' as const, text: `Error: ${data.error}` }] }; const entries = data.entries || data.memories || []; if (!entries.length) return { content: [{ type: 'text' as const, text: 'No agent memory entries found. Note: agent_memory_query browses by type/tags. Use "search" parameter for text search.' }] }; const text = entries.map((k: any) => `### ${k.title} (${k.type})\nID: ${k.id}\n${k.content}\n_Source: ${k.source || 'unknown'} | Confidence: ${k.confidence} | Tags: ${k.tags?.join(', ') || 'none'}_` ).join('\n\n'); return { content: [{ type: 'text' as const, text: `Agent memory (${entries.length}):\n\n${text}` }] }; } ); - src/index.ts:163-193 (helper)API helper functions (apiGet, apiPost, apiPatch, apiDelete) used by the handler to make HTTP requests to the AgentBay backend.
async function apiGet(path: string) { const res = await fetch(`${API_BASE}${path}`, { headers: getHeaders() }); return res.json(); } async function apiPost(path: string, body: unknown) { const res = await fetch(`${API_BASE}${path}`, { method: 'POST', headers: getHeaders(), body: JSON.stringify(body), }); return res.json(); } async function apiPatch(path: string, body: unknown) { const res = await fetch(`${API_BASE}${path}`, { method: 'PATCH', headers: getHeaders(), body: JSON.stringify(body), }); return res.json(); } async function apiDelete(path: string, body?: unknown) { const res = await fetch(`${API_BASE}${path}`, { method: 'DELETE', headers: getHeaders(), ...(body ? { body: JSON.stringify(body) } : {}), }); return res.json(); } - src/index.ts:86-92 (helper)getHeaders() helper that provides auth and content-type headers used by all API calls including agent_memory_query.
function getHeaders() { return { 'Authorization': `Bearer ${API_KEY}`, 'Content-Type': 'application/json', 'X-Agent-Framework': 'mcp-server', }; }