From 8cbb5788639607982d3609b9e756fabbce4a7f31 Mon Sep 17 00:00:00 2001
From: michael <1+michael@noreply.git.casaderoll.de>
Date: Mon, 7 Sep 2026 17:20:58 +0200
Subject: [PATCH 01/28] chore: .gitignore (bin/obj/dist)
---
.gitignore | 5 +++++
1 file changed, 5 insertions(+)
create mode 100644 .gitignore
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4e6a2f6
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+bin/
+obj/
+dist/
+*.user
+.vs/
From 06d79c9c06972496ed15bd080451f8e7ee935239 Mon Sep 17 00:00:00 2001
From: michael <1+michael@noreply.git.casaderoll.de>
Date: Mon, 7 Sep 2026 17:20:58 +0200
Subject: [PATCH 02/28] feat: eMPCby.csproj (net6.0, MediaBrowser.Server.Core
4.9.1.90)
---
src/eMPCby/eMPCby.csproj | 14 ++++++++++++++
1 file changed, 14 insertions(+)
create mode 100644 src/eMPCby/eMPCby.csproj
diff --git a/src/eMPCby/eMPCby.csproj b/src/eMPCby/eMPCby.csproj
new file mode 100644
index 0000000..59eedd0
--- /dev/null
+++ b/src/eMPCby/eMPCby.csproj
@@ -0,0 +1,14 @@
+
+
+ net6.0
+ eMPCby
+ eMPCby
+ 1.0.0
+ 1.0.0
+ false
+ CS1591
+
+
+
+
+
From 8fbf2a78b772df19a2ce5704066d4285bff4e01d Mon Sep 17 00:00:00 2001
From: michael <1+michael@noreply.git.casaderoll.de>
Date: Mon, 7 Sep 2026 17:20:59 +0200
Subject: [PATCH 03/28] feat: PluginConfiguration (Port, ListenAddress, ApiKey)
---
src/eMPCby/PluginConfiguration.cs | 12 ++++++++++++
1 file changed, 12 insertions(+)
create mode 100644 src/eMPCby/PluginConfiguration.cs
diff --git a/src/eMPCby/PluginConfiguration.cs b/src/eMPCby/PluginConfiguration.cs
new file mode 100644
index 0000000..302b454
--- /dev/null
+++ b/src/eMPCby/PluginConfiguration.cs
@@ -0,0 +1,12 @@
+using MediaBrowser.Model.Plugins;
+
+namespace eMPCby
+{
+ public class PluginConfiguration : BasePluginConfiguration
+ {
+ public bool EnableMcpServer { get; set; } = true;
+ public int McpPort { get; set; } = 8097;
+ public string ListenAddress { get; set; } = "0.0.0.0";
+ public string ApiKey { get; set; } = string.Empty; // leer = offen (nur LAN)
+ }
+}
From 6523573057b0071b61fd01cf2d66dc98a5efc528 Mon Sep 17 00:00:00 2001
From: michael <1+michael@noreply.git.casaderoll.de>
Date: Mon, 7 Sep 2026 17:20:59 +0200
Subject: [PATCH 04/28] feat: Plugin (BasePlugin + IServerEntryPoint, startet
MCP-Server in Run())
---
src/eMPCby/Plugin.cs | 63 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 63 insertions(+)
create mode 100644 src/eMPCby/Plugin.cs
diff --git a/src/eMPCby/Plugin.cs b/src/eMPCby/Plugin.cs
new file mode 100644
index 0000000..b1cde7c
--- /dev/null
+++ b/src/eMPCby/Plugin.cs
@@ -0,0 +1,63 @@
+using System;
+using MediaBrowser.Common.Configuration;
+using MediaBrowser.Common.Plugins;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Controller.Plugins;
+using MediaBrowser.Controller.Session;
+using MediaBrowser.Model.Logging;
+using MediaBrowser.Model.Serialization;
+using eMPCby.Mcp;
+
+namespace eMPCby
+{
+ public class Plugin : BasePlugin, IServerEntryPoint
+ {
+ private readonly ILogger _logger;
+ private readonly IJsonSerializer _jsonSerializer;
+ private McpHttpServer _mcpServer;
+
+ public static Plugin Instance { get; private set; }
+
+ // DI-Brücke: werden von ServiceRegistrar (zweites IServerEntryPoint) gesetzt
+ public ISessionManager SessionManager { get; set; }
+ public ILibraryManager LibraryManager { get; set; }
+ public IUserManager UserManager { get; set; }
+
+ public Plugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer,
+ ILogManager logManager, IJsonSerializer jsonSerializer)
+ : base(applicationPaths, xmlSerializer)
+ {
+ Instance = this;
+ _logger = logManager.GetLogger(GetType().Name);
+ _jsonSerializer = jsonSerializer;
+ }
+
+ public override string Name => "eMPCby";
+
+ public override string Description =>
+ "MCP-Server (Model Context Protocol) für Emby – Emby-Funktionen als MCP-Tools für AI-Agenten.";
+
+ // Feste Plugin-GUID – NIE ändern (Config-Zuordnung in Emby)
+ public override Guid Id => new Guid("095db6d6-5ecc-4ec3-85ea-7756dd6d958c");
+
+ public void Run()
+ {
+ var cfg = Configuration;
+ if (!cfg.EnableMcpServer)
+ {
+ _logger.Info("[eMPCby] MCP-Server ist deaktiviert.");
+ return;
+ }
+
+ _mcpServer = new McpHttpServer(_logger, _jsonSerializer, cfg.McpPort, cfg.ListenAddress, cfg.ApiKey);
+ var status = _mcpServer.Start();
+ _logger.Info("[eMPCby] MCP-Server Status: {0}", status);
+ }
+
+ public void Dispose()
+ {
+ _mcpServer?.Stop();
+ _mcpServer = null;
+ }
+ }
+}
From fbd037876cdd990ea87da4113d4126744a6b3dc8 Mon Sep 17 00:00:00 2001
From: michael <1+michael@noreply.git.casaderoll.de>
Date: Mon, 7 Sep 2026 17:21:00 +0200
Subject: [PATCH 05/28] =?UTF-8?q?feat:=20ServiceRegistrar=20(DI-Br=C3=BCck?=
=?UTF-8?q?e=20f=C3=BCr=20Session/Library/UserManager)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/eMPCby/ServiceRegistrar.cs | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
create mode 100644 src/eMPCby/ServiceRegistrar.cs
diff --git a/src/eMPCby/ServiceRegistrar.cs b/src/eMPCby/ServiceRegistrar.cs
new file mode 100644
index 0000000..7baaf6a
--- /dev/null
+++ b/src/eMPCby/ServiceRegistrar.cs
@@ -0,0 +1,27 @@
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Controller.Plugins;
+using MediaBrowser.Controller.Session;
+
+namespace eMPCby
+{
+ ///
+ /// Zweites IServerEntryPoint: Emby injiziert die Dienste per Konstruktor und
+ /// legt sie auf Plugin.Instance ab, damit die McpToolRegistry darauf zugreifen kann.
+ ///
+ public class ServiceRegistrar : IServerEntryPoint
+ {
+ public ServiceRegistrar(ISessionManager sessionManager, ILibraryManager libraryManager, IUserManager userManager)
+ {
+ if (Plugin.Instance != null)
+ {
+ Plugin.Instance.SessionManager = sessionManager;
+ Plugin.Instance.LibraryManager = libraryManager;
+ Plugin.Instance.UserManager = userManager;
+ }
+ }
+
+ public void Run() { }
+
+ public void Dispose() { }
+ }
+}
From 8198bfb7e7acb09dc5b0e666038afb3d7b13b17c Mon Sep 17 00:00:00 2001
From: michael <1+michael@noreply.git.casaderoll.de>
Date: Mon, 7 Sep 2026 17:22:46 +0200
Subject: [PATCH 06/28] feat: McpHttpServer (HttpListener auf /mcp,
API-Key-Check, JSON-RPC-Dispatch)
---
src/eMPCby/Mcp/McpHttpServer.cs | 154 ++++++++++++++++++++++++++++++++
1 file changed, 154 insertions(+)
create mode 100644 src/eMPCby/Mcp/McpHttpServer.cs
diff --git a/src/eMPCby/Mcp/McpHttpServer.cs b/src/eMPCby/Mcp/McpHttpServer.cs
new file mode 100644
index 0000000..48babaf
--- /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();
+ }
+ }
+}
From be541dd83e7c44ddba8d4a7a03ba88a2e9f984a0 Mon Sep 17 00:00:00 2001
From: michael <1+michael@noreply.git.casaderoll.de>
Date: Mon, 7 Sep 2026 17:22:47 +0200
Subject: [PATCH 07/28] feat: McpToolRegistry (4 Tools: server_info, search,
now_playing, get_item)
---
src/eMPCby/Mcp/McpToolRegistry.cs | 307 ++++++++++++++++++++++++++++++
1 file changed, 307 insertions(+)
create mode 100644 src/eMPCby/Mcp/McpToolRegistry.cs
diff --git a/src/eMPCby/Mcp/McpToolRegistry.cs b/src/eMPCby/Mcp/McpToolRegistry.cs
new file mode 100644
index 0000000..9fa7f58
--- /dev/null
+++ b/src/eMPCby/Mcp/McpToolRegistry.cs
@@ -0,0 +1,307 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text.Json;
+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.
+ /// 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;
+ }
+
+ 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)
+ {
+ 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 = "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, 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("[eMPCby] 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