diff --git a/src/eMPCby/Mcp/McpHttpServer.cs b/src/eMPCby/Mcp/McpHttpServer.cs
new file mode 100644
index 0000000..1c488d9
--- /dev/null
+++ b/src/eMPCby/Mcp/McpHttpServer.cs
@@ -0,0 +1,154 @@
+using System;
+using System.IO;
+using System.Net;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using MediaBrowser.Model.Logging;
+using MediaBrowser.Model.Serialization;
+
+namespace eMPCby.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("[eMPCby] 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("[eMPCby] 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("[eMPCby] 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("[eMPCby] 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/eMPCby/Mcp/McpToolRegistry.cs b/src/eMPCby/Mcp/McpToolRegistry.cs
new file mode 100644
index 0000000..5356fb0
--- /dev/null
+++ b/src/eMPCby/Mcp/McpToolRegistry.cs
@@ -0,0 +1,276 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Controller.Session;
+using MediaBrowser.Model.Logging;
+using MediaBrowser.Model.Serialization;
+
+namespace eMPCby.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.
+ ///
+ public class McpToolRegistry
+ {
+ private readonly ILogger _logger;
+ private readonly IJsonSerializer _json;
+
+ public McpToolRegistry(ILogger logger, IJsonSerializer json)
+ {
+ _logger = logger;
+ _json = json;
+ }
+
+ private ISessionManager Sessions => Plugin.Instance?.SessionManager;
+ private ILibraryManager Library => Plugin.Instance?.LibraryManager;
+ private IUserManager Users => Plugin.Instance?.UserManager;
+
+ // ---------- JSON-RPC 2.0 Handler ----------
+
+ public object HandleRequest(string body)
+ {
+ Dictionary req;
+ try
+ {
+ req = _json.DeserializeFromString(body, typeof(Dictionary)) as Dictionary;
+ }
+ catch (Exception ex)
+ {
+ return RpcError(null, -32700, "Parse error: " + ex.Message);
+ }
+
+ if (req == null)
+ return RpcError(null, -32700, "Parse error: Request ist kein JSON-Objekt");
+
+ var id = req.TryGetValue("id", out var idVal) ? idVal : null;
+ var method = req.TryGetValue("method", out var mVal) ? mVal?.ToString() : null;
+ var paramsDict = req.TryGetValue("params", out var pVal) ? pVal as Dictionary : null;
+
+ switch (method)
+ {
+ case "initialize":
+ return RpcResult(id, new
+ {
+ protocolVersion = "2024-11-05",
+ serverInfo = new { name = "eMPCby", 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, paramsDict);
+
+ default:
+ return RpcError(id, -32601, "Methode nicht gefunden: " + method);
+ }
+ }
+
+ private object HandleToolCall(object id, Dictionary p)
+ {
+ var name = p != null && p.TryGetValue("name", out var nVal) ? nVal?.ToString() : null;
+ var args = p != null && p.TryGetValue("arguments", out var aVal)
+ ? aVal as Dictionary
+ : new Dictionary();
+
+ try
+ {
+ object result;
+ switch (name)
+ {
+ case "emby_server_info":
+ result = ToolServerInfo();
+ break;
+ case "emby_search":
+ result = ToolSearch(args);
+ break;
+ case "emby_now_playing":
+ result = ToolNowPlaying();
+ break;
+ case "emby_get_item":
+ result = ToolGetItem(args);
+ 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("[eMPCby] Tool-Fehler: " + name, ex);
+ return RpcResult(id, new
+ {
+ content = new[] { new { type = "text", text = "Fehler: " + ex.Message } },
+ isError = true
+ });
+ }
+ }
+
+ // ---------- Tool-Definitionen ----------
+
+ private List