Open2

GoLang , remote MCP Server 作成 JSON-RPC 2.0

knaka Tech-Blogknaka Tech-Blog

概要

  • GoLang , remote MCP Server メモです。
  • json-rpc 2.0 型式で、POST通信

[ 公開 2025/10/15 ]


環境

  • go version go1.24.4 windows/amd64
  • MCP Server
  • node 22

参考したコード

https://gist.github.com/kuc-arc-f/058ff841268ad3a96c0731ce72cf3340


  • dev-start
go mod init example.com/go-remote-mcp-server5
go mod tidy

go build
go run .

  • settings.json : GEMINI-CLI
  • httpUrl: mcpのエンドポイント
    "myRemoteServer": {
      "httpUrl": "http://localhost:8080/mcp", 
      "headers": {
        "Authorization": "Bearer YOUR_ACCESS_TOKEN" 
      },
      "timeout": 5000 
    }      

  • 入力文
Echo back the input message , Hello!

  • 実行結果の確認
  • 入力文字が、返却される


  • test-code

  • test1.js : tools/list リスト返却

  • test.js


const start = async function() {
  try{
      const item = {
        "jsonrpc": "2.0",
        "method": "tools/list",
        "id": 2
      }
      const response = await fetch("http://localhost:8080/mcp", {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': '',
      },
      body: JSON.stringify(item),
    });
    if (!response.ok) {
      const text = await response.text();
      console.log(text);
      throw new Error('Failed to create item');
    }else{
      const json = await response.json();
      console.log(json);
      console.log(json.result.tools);

    }
  }catch(e){console.log(e)}
}
start();



  • test2.js

const start = async function() {
  try{
      const item = {
        "jsonrpc": "2.0",
        "method": "tools/call",
        "params": {
          "name": "echo",
          "arguments": {
            "message": "Hello, MCP!"
          }
        },
        "id": 3
      }
      const response = await fetch("http://localhost:8080/mcp", {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': '',
      },
      body: JSON.stringify(item),
    });
    if (!response.ok) {
      const text = await response.text();
      console.log(text);
      throw new Error('Failed to create item');
    }else{
      const json = await response.json();
      console.log(json);
      console.log(json.result.content[0].text);

    }
  }catch(e){console.log(e)}
}
start();


  • テスト結果
node test2.js
{ jsonrpc: '2.0', result: { content: [ [Object] ] }, id: 3 }
Hello, MCP!