import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
/**
* Example TypeScript client demonstrating "Code Mode" integration with RLM-MCP.
*
* This client shows how to:
* 1. Connect to the RLM MCP server via stdio.
* 2. Invoke the 'solve' tool.
* 3. Consume structured content (answer_json) for type-safe processing.
*/
async function main() {
// 1. Setup transport - change to your actual server path
const transport = new StdioClientTransport({
command: "uv",
args: ["run", "python", "-u", "rlm_mcp_server/server.py"],
});
const client = new Client(
{
name: "rlm-example-client",
version: "1.0.0",
},
{
capabilities: {},
}
);
await client.connect(transport);
// 2. List tools (optional)
const tools = await client.listTools();
console.log("Available tools:", tools.tools.map(t => t.name));
// 3. Call 'solve' tool
const query = "Analyze the codebase for potential security risks in the hashing logic. Return a JSON list of risks.";
console.log(`\nCalling 'solve' with query: "${query}"...`);
const result = await client.callTool({
name: "solve",
arguments: {
query: query,
globs: ["rlm_mcp_server/*.py"],
rlm: {
model_name: "openai/gpt-4o-mini", // or any other supported model
}
}
});
// 4. Handle Result
// The server returns a CallToolResult.
// Legacy/Text clients look at result.content[0].text
// "Code Mode" / Typed clients look at the 'structured_content' in meta.
const textAnswer = result.content
.filter(c => c.type === "text")
.map(c => (c as any).text)
.join("\n");
console.log("\n--- Human Readable Answer ---");
console.log(textAnswer);
if (result._meta && (result._meta as any).structured_content) {
const structured = (result._meta as any).structured_content;
const answerJson = structured.answer_json;
console.log("\n--- Typed Answer (JSON) ---");
console.log(JSON.stringify(answerJson, null, 2));
if (Array.isArray(answerJson)) {
console.log(`\nFound ${answerJson.length} risk items in structured output.`);
}
} else {
console.log("\nNote: No structured content found in metadata.");
}
process.exit(0);
}
main().catch((error) => {
console.error("Error:", error);
process.exit(1);
});