概要
MCPサーバーが気になったので、調べてみた。
AIに、聞いてみた。
自作MCPサーバー、書いてみた。
参考にしたページ
写真
サンプルコード
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using LitJson;
namespace HumanLlmMcpServer
{
class Program {
static string AskHumanViaNotepad(string prompt) {
string tempFile = Path.Combine(Path.GetTempPath(), "humanllm_" + Guid.NewGuid().ToString("N") + ".txt");
string marker = "=== ここに回答を書いて保存(Ctrl+S)し、メモ帳を閉じてください ===";
string template = "=== Claudeからのプロンプト ===\r\n" + prompt + "\r\n\r\n" + marker + "\r\n";
File.WriteAllText(tempFile, template, new UTF8Encoding(true));
try
{
ProcessStartInfo psi = new ProcessStartInfo("notepad.exe", "\"" + tempFile + "\"") {
UseShellExecute = true
};
using (Process proc = Process.Start(psi))
{
if (proc != null)
{
proc.WaitForExit();
}
}
string content = File.ReadAllText(tempFile, Encoding.UTF8);
int idx = content.IndexOf(marker);
if (idx >= 0)
{
string reply = content.Substring(idx + marker.Length).Trim();
return string.IsNullOrEmpty(reply) ? "(空の回答が送信されました)" : reply;
}
return content.Trim();
}
finally
{
if (File.Exists(tempFile))
{
try
{
File.Delete(tempFile);
}
catch
{
}
}
}
}
static void SendResult(JsonData id, object result) {
var res = new JsonData();
res["jsonrpc"] = "2.0";
res["id"] = id;
res["result"] = JsonMapper.ToObject(JsonMapper.ToJson(result));
WriteJsonLine(res);
}
static void SendError(JsonData id, int code, string message) {
var err = new JsonData();
err["code"] = code;
err["message"] = message;
var res = new JsonData();
res["jsonrpc"] = "2.0";
res["id"] = id;
res["error"] = err;
WriteJsonLine(res);
}
static void WriteJsonLine(JsonData data) {
string json = JsonMapper.ToJson(data);
Console.WriteLine(json);
Console.Out.Flush();
}
static void Main(string[] args) {
Console.InputEncoding = new UTF8Encoding(false);
Console.OutputEncoding = new UTF8Encoding(false);
string line;
while ((line = Console.ReadLine()) != null)
{
if (string.IsNullOrWhiteSpace(line))
continue;
try
{
JsonData req = JsonMapper.ToObject(line);
if (!req.Keys.Contains("id") || req["id"] == null)
continue;
JsonData id = req["id"];
string method = req.Keys.Contains("method") ? (string) req["method"] : "";
switch (method)
{
case "initialize":
var initResult = new Dictionary<string, object> {
{
"protocolVersion", "2024-11-05"
}, {
"capabilities", new Dictionary<string, object> {
{
"tools", new Dictionary<string, object>()
}
}
}, {
"serverInfo", new Dictionary<string, object> {
{
"name", "human-llm-server"
}, {
"version", "1.0.0"
}
}
}
};
SendResult(id, initResult);
break;
case "tools/list":
var toolItem = new Dictionary<string, object> {
{
"name", "ask_human"
}, {
"description", "LLMの代わりに人間がプロンプトに回答します。質問やタスクの解決が必要なときは必ずこのツールを呼び出してください。"
}, {
"inputSchema", new Dictionary<string, object> {
{
"type", "object"
}, {
"properties", new Dictionary<string, object> {
{
"prompt", new Dictionary<string, object> {
{
"type", "string"
}, {
"description", "人間に答えてほしい質問・指示内容"
}
}
}
}
}, {
"required", new string[] {
"prompt"
}
}
}
}
};
var listResult = new Dictionary<string, object> {
{
"tools", new object[] {
toolItem
}
}
};
SendResult(id, listResult);
break;
case "tools/call":
string prompt = "";
if (req.Keys.Contains("params") && req["params"].Keys.Contains("arguments"))
{
JsonData arguments = req["params"]["arguments"];
if (arguments.Keys.Contains("prompt"))
{
prompt = (string) arguments["prompt"];
}
}
string humanReply = AskHumanViaNotepad(prompt);
var contentItem = new Dictionary<string, object> {
{
"type", "text"
}, {
"text", humanReply
}
};
var callResult = new Dictionary<string, object> {
{
"content", new object[] {
contentItem
}
}
};
SendResult(id, callResult);
break;
default:
SendError(id, -32601, "Method not found");
break;
}
}
catch (Exception ex)
{
Console.Error.WriteLine("MCP Server Error: " + ex.Message);
}
}
}
}
}
セッティング
"mcpServers": {
"human-llm-server": {
"command": "C:\\Users\\ohisa\\csc\\human2.exe"
}
},
以上。
