Skip to main content
Glama
test-client.js6.07 kB
import { Client } from "@modelcontextprotocol/sdk/client/index.js"; import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"; async function main() { // Create client transport const transport = new StdioClientTransport({ command: "node", args: ["dist/server.js"] }); // Create client instance const client = new Client({ name: "mem0-test-client", version: "1.0.0" }, { capabilities: { prompts: {}, resources: {}, tools: {} } }); try { // Connect to server await client.connect(transport); console.log("Connected to mem0 MCP server"); // Test 1: Create a memory stream with user preferences console.log("\nTest 1: Creating memory stream with user preferences"); const createResult = await client.callTool({ name: "create-memory-stream", arguments: { name: "user-preferences", initialContent: "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts.", userId: "test-user-1", agentId: "test-agent-1" } }); console.log("Create result:", createResult); // Parse the stream ID const response = createResult.content?.[0]?.text; if (!response) throw new Error("Invalid create response"); const { streamId } = JSON.parse(response); // Test 2: Append multiple messages to simulate a conversation console.log("\nTest 2: Appending conversation messages"); const messages = [ { role: "assistant", content: "Hello Alex! I understand you're vegetarian and have a nut allergy. I'll keep that in mind. What kind of cuisine do you enjoy?" }, { role: "user", content: "I love Italian and Indian food, especially vegetarian pasta dishes and dal." }, { role: "assistant", content: "Great choices! Both Italian and Indian cuisines have excellent vegetarian options. For Italian, we can explore dishes like pasta primavera or mushroom risotto. For Indian, besides dal, there's paneer dishes (without cashew sauce), vegetable curries, and many lentil-based options." } ]; for (const msg of messages) { console.log(`\nAppending ${msg.role} message:`, msg.content); const appendResult = await client.callTool({ name: "append-to-stream", arguments: { streamId, content: msg.content, role: msg.role } }); console.log("Append result:", appendResult); } // Test 3: Multiple memory searches with different queries console.log("\nTest 3: Testing memory searches"); const searchQueries = [ "What are Alex's dietary restrictions?", "What kind of food does Alex like?", "What cuisines does Alex enjoy?", "Does Alex have any allergies?" ]; for (const query of searchQueries) { console.log(`\nSearching: "${query}"`); const searchResult = await client.callTool({ name: "search-memories", arguments: { query, userId: "test-user-1", agentId: "test-agent-1", threshold: 0.1 } }); console.log("Search result:", searchResult); } // Test 4: Read entire conversation console.log("\nTest 4: Reading entire conversation"); const readResult = await client.callTool({ name: "read-stream", arguments: { streamId, startIndex: 0 } }); console.log("Full conversation:", readResult); // Test 5: Create another stream for a different user console.log("\nTest 5: Creating stream for different user"); const createResult2 = await client.callTool({ name: "create-memory-stream", arguments: { name: "travel-preferences", initialContent: "I'm Sarah and I love adventure travel and hiking.", userId: "test-user-2", agentId: "test-agent-1" } }); console.log("Create result for second user:", createResult2); // Test 6: Search across both users console.log("\nTest 6: Searching across users"); const globalSearch = await client.callTool({ name: "search-memories", arguments: { query: "What do we know about user preferences?", userId: "test-user-1", // Primary user threshold: 0.1 } }); console.log("Global search result:", globalSearch); // Test 7: Clean up - delete streams console.log("\nTest 7: Cleaning up streams"); const streams = await client.readResource({ uri: "memory://" }); console.log("Current streams:", streams); const streamList = JSON.parse(streams.contents[0].text); for (const stream of streamList) { console.log(`\nDeleting stream: ${stream.id}`); const deleteResult = await client.callTool({ name: "delete-stream", arguments: { streamId: stream.id } }); console.log("Delete result:", deleteResult); } // Verify deletion console.log("\nVerifying all streams deleted:"); const finalStreams = await client.readResource({ uri: "memory://" }); console.log("Final streams:", finalStreams); } catch (error) { console.error("Error:", error); } finally { await transport.close(); } } main().catch(console.error);

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/sadiuysal/mem0-mcp-server-ts'

If you have feedback or need assistance with the MCP directory API, please join our Discord server