diff --git a/src/eMCPy/Mcp/McpHttpServer.cs b/src/eMCPy/Mcp/McpHttpServer.cs
index 769c939..b9a623b 100644
--- a/src/eMCPy/Mcp/McpHttpServer.cs
+++ b/src/eMCPy/Mcp/McpHttpServer.cs
@@ -7,4 +7,148 @@ using System.Threading.Tasks;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
-nam...[truncated]
\ No newline at end of file
+namespace eMCPy.Mcp
+{
+ ///
+ /// Eigenständiger HTTP-Listener (Muster: WatchParty ExternalWebServer), der das
+ /// MCP-Protokoll (JSON-RPC 2.0) auf /mcp bedient. Läuft neben dem Emby-Port.
+ ///
+ public class McpHttpServer
+ {
+ private readonly ILogger _logger;
+ private readonly IJsonSerializer _json;
+ private readonly int _port;
+ private readonly string _listenAddress;
+ private readonly string _apiKey;
+ private readonly HttpListener _listener;
+ private readonly CancellationTokenSource _cts = new CancellationTokenSource();
+ private readonly McpToolRegistry _tools;
+
+ public McpHttpServer(ILogger logger, IJsonSerializer json, int port, string listenAddress, string apiKey)
+ {
+ _logger = logger;
+ _json = json;
+ _port = port;
+ _listenAddress = string.IsNullOrEmpty(listenAddress) ? "0.0.0.0" : listenAddress;
+ _apiKey = apiKey ?? string.Empty;
+ _listener = new HttpListener();
+ // "*" bindet an alle Interfaces (Linux: ohne Root-Problem)
+ _listener.Prefixes.Add($"http://*:{_port}/");
+ _tools = new McpToolRegistry(_logger, _json);
+ }
+
+ public string Start()
+ {
+ try
+ {
+ _listener.Start();
+ _logger.Info("[eMCPy] MCP-Server lauscht auf http://{0}:{1}/mcp", _listenAddress, _port);
+ Task.Run(() => ListenAsync(_cts.Token));
+ return $"Lauscht auf {_listenAddress}:{_port}";
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("[eMCPy] MCP-Server konnte nicht starten", ex);
+ return "Fehler: " + ex.Message;
+ }
+ }
+
+ public void Stop()
+ {
+ if (!_listener.IsListening) return;
+ _cts.Cancel();
+ _listener.Stop();
+ _listener.Close();
+ _logger.Info("[eMCPy] MCP-Server gestoppt.");
+ }
+
+ private async Task ListenAsync(CancellationToken ct)
+ {
+ while (!ct.IsCancellationRequested && _listener.IsListening)
+ {
+ HttpListenerContext ctx;
+ try
+ {
+ ctx = await _listener.GetContextAsync().ConfigureAwait(false);
+ }
+ catch
+ {
+ break; // Listener gestoppt
+ }
+ _ = Task.Run(() => HandleAsync(ctx));
+ }
+ }
+
+ private async Task HandleAsync(HttpListenerContext ctx)
+ {
+ try
+ {
+ var req = ctx.Request;
+ var resp = ctx.Response;
+ resp.ContentType = "application/json";
+
+ if (req.HttpMethod != "POST" ||
+ !req.Url.AbsolutePath.StartsWith("/mcp", StringComparison.OrdinalIgnoreCase))
+ {
+ resp.StatusCode = 404;
+ await WriteJsonAsync(resp, new { error = "Nicht gefunden. Nutze POST /mcp" });
+ return;
+ }
+
+ // API-Key-Check (Header "X-Api-Key" oder "Authorization: Bearer ***")
+ if (!string.IsNullOrEmpty(_apiKey))
+ {
+ var key = req.Headers["X-Api-Key"];
+ if (string.IsNullOrEmpty(key))
+ {
+ var auth = req.Headers["Authorization"];
+ if (auth != null && auth.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
+ key = auth.Substring(7);
+ }
+ if (key != _apiKey)
+ {
+ resp.StatusCode = 401;
+ await WriteJsonAsync(resp, new { error = "Ungültiger API-Key" });
+ return;
+ }
+ }
+
+ using var reader = new StreamReader(req.InputStream, req.ContentEncoding);
+ var body = await reader.ReadToEndAsync();
+ var result = _tools.HandleRequest(body);
+
+ if (result == null)
+ {
+ // Notification (z. B. notifications/initialized) → 202, kein Body
+ resp.StatusCode = 202;
+ resp.Close();
+ return;
+ }
+
+ await WriteJsonAsync(resp, result);
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("[eMCPy] Fehler bei MCP-Request", ex);
+ try
+ {
+ ctx.Response.StatusCode = 500;
+ await WriteJsonAsync(ctx.Response, new { error = ex.Message });
+ }
+ catch
+ {
+ // Response bereits geschlossen
+ }
+ }
+ }
+
+ private async Task WriteJsonAsync(HttpListenerResponse resp, object obj)
+ {
+ var json = _json.SerializeToString(obj);
+ var bytes = Encoding.UTF8.GetBytes(json);
+ resp.ContentLength64 = bytes.Length;
+ await resp.OutputStream.WriteAsync(bytes, 0, bytes.Length);
+ resp.Close();
+ }
+ }
+}
diff --git a/src/eMCPy/Mcp/McpToolRegistry.cs b/src/eMCPy/Mcp/McpToolRegistry.cs
index fdbcd3c..b8e53fc 100644
--- a/src/eMCPy/Mcp/McpToolRegistry.cs
+++ b/src/eMCPy/Mcp/McpToolRegistry.cs
@@ -4,4 +4,313 @@ using System.Linq;
using System.Text.Json;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Session;
-using MediaBrowser.Model.Loggin...[truncated]
\ No newline at end of file
+using MediaBrowser.Model.Logging;
+using MediaBrowser.Model.Serialization;
+
+namespace eMCPy.Mcp
+{
+ ///
+ /// MCP-Tool-Registry: parst JSON-RPC 2.0 Requests (initialize, tools/list, tools/call)
+ /// und implementiert die Emby-Tools direkt über die injizierten Emby-Dienste.
+ /// Request-Parsing läuft über System.Text.Json (robust gegenüber Emby's Newtonsoft-Serializer).
+ ///
+ public class McpToolRegistry
+ {
+ private readonly ILogger _logger;
+ private readonly IJsonSerializer _json;
+
+ public McpToolRegistry(ILogger logger, IJsonSerializer json)
+ {
+ _logger = logger;
+ _json = json;
+ }
+
+ // DI-Brücke: werden von ServiceRegistrar gesetzt
+ public ISessionManager Sessions { get; set; }
+ public ILibraryManager Library { get; set; }
+ public IUserManager Users { get; set; }
+
+ public object HandleRequest(string body)
+ {
+ JsonElement root;
+ try
+ {
+ using (var doc = JsonDocument.Parse(body))
+ {
+ root = doc.RootElement.Clone();
+ }
+ }
+ catch (Exception ex)
+ {
+ return RpcError(default, -32700, "Parse error: " + ex.Message);
+ }
+
+ if (root.ValueKind != JsonValueKind.Object)
+ return RpcError(default, -32700, "Parse error: Request ist kein JSON-Objekt");
+
+ var id = root.TryGetProperty("id", out var idEl) ? idEl.Clone() : default;
+ var method = root.TryGetProperty("method", out var mEl) && mEl.ValueKind == JsonValueKind.String
+ ? mEl.GetString() : null;
+ var hasParams = root.TryGetProperty("params", out var pEl) && pEl.ValueKind == JsonValueKind.Object;
+
+ switch (method)
+ {
+ case "initialize":
+ return RpcResult(id, new
+ {
+ protocolVersion = "2024-11-05",
+ serverInfo = new { name = "eMCPy", version = "1.0.0" },
+ capabilities = new { tools = new { listChanged = false } }
+ });
+
+ case "notifications/initialized":
+ return null; // Notification → keine Antwort
+
+ case "tools/list":
+ return RpcResult(id, new { tools = GetToolDefinitions() });
+
+ case "tools/call":
+ return HandleToolCall(id, hasParams ? pEl : default);
+
+ default:
+ return RpcError(id, -32601, "Methode nicht gefunden: " + method);
+ }
+ }
+
+ private object HandleToolCall(JsonElement id, JsonElement p)
+ {
+ var name = p.ValueKind == JsonValueKind.Object && p.TryGetProperty("name", out var nEl)
+ && nEl.ValueKind == JsonValueKind.String ? nEl.GetString() : null;
+ var args = p.ValueKind == JsonValueKind.Object && p.TryGetProperty("arguments", out var aEl)
+ && aEl.ValueKind == JsonValueKind.Object ? aEl : default;
+ var hasArgs = args.ValueKind == JsonValueKind.Object;
+
+ try
+ {
+ object result;
+ switch (name)
+ {
+ case "emby_server_info":
+ result = ToolServerInfo();
+ break;
+ case "emby_search":
+ result = ToolSearch(hasArgs ? args : default);
+ break;
+ case "emby_now_playing":
+ result = ToolNowPlaying();
+ break;
+ case "emby_get_item":
+ result = ToolGetItem(hasArgs ? args : default);
+ break;
+ default:
+ return RpcError(id, -32602, "Unbekanntes Tool: " + name);
+ }
+
+ return RpcResult(id, new
+ {
+ content = new[]
+ {
+ new { type = "text", text = _json.SerializeToString(result) }
+ },
+ isError = false
+ });
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("[eMCPy] Tool-Fehler: " + name, ex);
+ return RpcResult(id, new
+ {
+ content = new[]
+ {
+ new { type = "text", text = "Fehler: " + ex.Message }
+ },
+ isError = true
+ });
+ }
+ }
+
+ private static string ArgString(JsonElement args, string key)
+ {
+ if (args.ValueKind == JsonValueKind.Object && args.TryGetProperty(key, out var v)
+ && v.ValueKind == JsonValueKind.String)
+ return v.GetString();
+ return null;
+ }
+
+ // ---------- Tool-Definitionen ----------
+
+ private List