Open1

node + Rust MCP Server , Postgresql 操作メモ

knaka Tech-Blogknaka Tech-Blog

概要

  • 前の node + Rust MCP Server 関連メモになります
  • Postgresql使用
  • json-rpc 2.0 で、操作します
  • 今回は、ホスティング等対応で。linux build (win WSL) です。

[ 公開 2025/11/05 ]


環境

  • node 22
  • rustc 1.90.0
  • MCP Server
  • Postgresql , supabase

関連

  • 前の MCP Server , Turso database

https://zenn.dev/knaka0209/scraps/4be37f5db128f0


参考したコード

https://github.com/kuc-arc-f/mcp_server_2ex/tree/main/mcp_app11

  • Rust MCP Server下記です。

https://github.com/kuc-arc-f/rust_3ex/tree/main/mcp_11


  • dev-start
  • dist フォルダ下に、Rust MCP Server 配置します。
npm run build
npm run dev

  • .env
USER_NAME="a1@example.com"
PASSWORD="1111"

  • create
  • mcp_app11/src-ts/routes/test.ts

https://github.com/kuc-arc-f/mcp_server_2ex/blob/main/mcp_app11/src-ts/routes/test.ts


router.post('/create', async function(req: any, res: any) {
  const retObj = {ret: 500, data: null};
  try {
    const body = req.body
    console.log(body);
    const client = new RpcClient(CMD_PATH);
    const resp = await client.call(
      "tools/call", 
      { 
        name: "test_create", 
        arguments:{
          title: body.title,
          content: body.content,
        }
         
      },
    );
    client.close();    
  //console.log("add:", resp);
   retObj.ret = 200;
   return res.json(retObj);
  } catch (error) {
    console.error(error);
    res.sendStatus(500);
  }
});
  • list

router.post('/list', async function(req: any, res: any) {
  const retObj = {ret: 500, data: null};
  try {
    const body = req.body
    console.log(body);
    const client = new RpcClient(CMD_PATH);
    const resp = await client.call(
      "tools/call", 
      { 
        name: "test_list", 
        arguments:{
          content: body.content,
        }          
      },
    );
    client.close();    
  //console.log("add:", resp);
    let out = [];
    if(resp.content[0]){
      const json = JSON.parse(resp.content[0].text)
      console.log(json)
      out = json;
    }

   retObj.ret = 200;
   retObj.data = out;
   return res.json(retObj);
  } catch (error) {
    console.error(error);
    res.sendStatus(500);
  }
});

  • rust

  • mcp_11/src/main.rs

  • tool 起動

https://github.com/kuc-arc-f/rust_3ex/blob/main/mcp_11/src/main.rs

async fn handle_request(request: JsonRpcRequest) -> JsonRpcResponse {
    match request.method.as_str() {
        "tools/call" => {
            if let Some(params) = request.params {
                if let Some(tool_name) = params.get("name").and_then(|v| v.as_str()) {
                    if tool_name == "test_create" {
                        mod_test::test_create_handler(params, request.id).await
                    } else if tool_name == "test_list" {
                        mod_test::test_list_handler(params, request.id).await
                    } else {
                        JsonRpcResponse {
                            jsonrpc: "2.0".to_string(),
                            id: request.id,
                            result: None,
                            error: Some(JsonRpcError {
                                code: -32601,
                                message: "Tool not found".to_string(),
                            }),
                        }
                    }
                } else{
                    JsonRpcResponse {
                        jsonrpc: "2.0".to_string(),
                        id: request.id,
                        result: None,
                        error: Some(JsonRpcError {
                            code: -32601,
                            message: "Tool not found".to_string(),
                        }),
                    }
                }
            } else {
                JsonRpcResponse {
                    jsonrpc: "2.0".to_string(),
                    id: request.id,
                    result: None,
                    error: Some(JsonRpcError {
                        code: -32601,
                        message: "arguments.name not found".to_string(),
                    }),
                }
            }
        }
        _ => JsonRpcResponse {
            jsonrpc: "2.0".to_string(),
            id: request.id,
            result: None,
            error: Some(JsonRpcError {
                code: -32601,
                message: "Method not found".to_string(),
            }),
        },
    }
}