Skip to content

Instantly share code, notes, and snippets.

@kuc-arc-f
Last active January 11, 2026 05:49
Show Gist options
  • Select an option

  • Save kuc-arc-f/33c1f4a3c423086e092d2328edbbaaab to your computer and use it in GitHub Desktop.

Select an option

Save kuc-arc-f/33c1f4a3c423086e092d2328edbbaaab to your computer and use it in GitHub Desktop.
MCP node RAG , Rust process start
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import RpcClient from "./RpcClient.js";
const server = new McpServer({
name: "mcp-example",
version: "1.0.0",
});
server.tool(
"rag_search",
"検索文字から、RAG検索 結果を返す。",
{query: z.string().min(1, { message: '検索文字は必須です' })},
async({query}) => {
const client = new RpcClient("/work/rust/extra/qdrant_3/target/debug/rust_qdrant_3.exe");
const resp = await client.call(
"tools/call",
{
name: "rag_search",
arguments:{
query: query
}
},
);
client.close();
console.log("add結果=", resp);
let out = "";
// @ts-ignore
if(resp.content[0].text){
// @ts-ignore
console.log("text:"+ resp.content[0].text);
// @ts-ignore
out = resp.content[0].text;
}
return {content: [{type: "text", text: out }]}
},
);
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Example MCP Server running on stdio");
}
main().catch((error) => {
console.error("Fatal error in main():", error);
process.exit(1);
});
{
"name": "mcp-example",
"version": "1.0.0",
"main": "index.js",
"type": "module",
"bin": {
"mcp-example": "./build/index.js"
},
"scripts": {
"build": "tsc",
"build:test": "tsc && chmod 755 build/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"files": [
"build"
],
"dependencies": {
"@modelcontextprotocol/sdk": "^1.9.0",
"zod": "^3.24.2"
},
"devDependencies": {
"@types/node": "^22.14.0",
"typescript": "^5.8.3"
}
}
import { spawn } from "child_process";
class RpcClient {
constructor(command) {
// @ts-ignore
this.proc = spawn(command);
// @ts-ignore
this.idCounter = 1;
// @ts-ignore
this.pending = new Map();
// @ts-ignore
this.proc.stdout.setEncoding("utf8");
// @ts-ignore
this.proc.stdout.on("data", (data) => this._handleData(data));
// @ts-ignore
this.proc.stderr.on("data", (err) => console.error("Rust stderr:", err.toString()));
// @ts-ignore
this.proc.on("exit", (code) => console.log(`Rust server exited (${code})`));
}
_handleData(data) {
// 複数行対応
data.split("\n").forEach((line) => {
if (!line.trim()) return;
try {
const msg = JSON.parse(line);
// @ts-ignore
if (msg.id && this.pending.has(msg.id)) {
// @ts-ignore
const { resolve } = this.pending.get(msg.id);
// @ts-ignore
this.pending.delete(msg.id);
resolve(msg.result);
}
} catch (e) {
//console.error("JSON parse error:", e, line);
}
});
}
call(method, params = {}) {
// @ts-ignore
const id = this.idCounter++;
const payload = {
jsonrpc: "2.0",
id,
method,
params,
};
return new Promise((resolve, reject) => {
// @ts-ignore
this.pending.set(id, { resolve, reject });
// @ts-ignore
this.proc.stdin.write(JSON.stringify(payload) + "\n");
});
}
close() {
// @ts-ignore
this.proc.kill();
}
}
export default RpcClient;
{
"compilerOptions": {
"target": "ES2022",
"module": "Node16",
"moduleResolution": "Node16",
"outDir": "./build",
"rootDir": "./src",
"strict": false,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment