|
1 | 1 | local mcphub = require("mcphub") |
2 | 2 | local prompt_utils = require("mcphub.utils.prompt") |
3 | 3 |
|
| 4 | +mcphub.add_tool("mcphub", { |
| 5 | + name = "get_current_servers", |
| 6 | + description = "Get the current state of all MCP servers (connected and disabled). This is useful when you need to know what servers are currently available, especially when restoring chat from history or when server state may have changed.", |
| 7 | + inputSchema = { |
| 8 | + type = "object", |
| 9 | + properties = { |
| 10 | + include_disabled = { |
| 11 | + type = "boolean", |
| 12 | + description = "Whether to include disabled servers in the response (default: true)", |
| 13 | + default = true, |
| 14 | + }, |
| 15 | + format = { |
| 16 | + type = "string", |
| 17 | + description = "Response format: 'detailed' for full server info or 'summary' for compact list (default: detailed)", |
| 18 | + enum = { "detailed", "summary" }, |
| 19 | + default = "detailed", |
| 20 | + }, |
| 21 | + }, |
| 22 | + }, |
| 23 | + handler = function(req, res) |
| 24 | + local hub = mcphub.get_hub_instance() |
| 25 | + if not hub or not hub:is_ready() then |
| 26 | + return res:error("Hub is not ready") |
| 27 | + end |
| 28 | + |
| 29 | + local include_disabled = req.params.include_disabled ~= false -- default to true |
| 30 | + local format = req.params.format or "detailed" |
| 31 | + |
| 32 | + if format == "summary" then |
| 33 | + local connected_count = 0 |
| 34 | + local disabled_count = 0 |
| 35 | + local server_names = { connected = {}, disabled = {} } |
| 36 | + |
| 37 | + local servers = hub:get_servers(include_disabled) |
| 38 | + for _, server in ipairs(servers) do |
| 39 | + if server.status == "connected" then |
| 40 | + connected_count = connected_count + 1 |
| 41 | + table.insert(server_names.connected, server.name) |
| 42 | + elseif server.status == "disabled" then |
| 43 | + disabled_count = disabled_count + 1 |
| 44 | + table.insert(server_names.disabled, server.name) |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + local summary = string.format( |
| 49 | + "# Current MCP Server Status\n\n" .. "Connected servers (%d): %s\n" .. "Disabled servers (%d): %s", |
| 50 | + connected_count, |
| 51 | + #server_names.connected > 0 and table.concat(server_names.connected, ", ") or "None", |
| 52 | + disabled_count, |
| 53 | + #server_names.disabled > 0 and table.concat(server_names.disabled, ", ") or "None" |
| 54 | + ) |
| 55 | + |
| 56 | + return res:text(summary):send() |
| 57 | + else |
| 58 | + -- Detailed format using the existing active servers prompt |
| 59 | + local detailed_info = hub:get_active_servers_prompt(true, include_disabled) |
| 60 | + return res:text(detailed_info):send() |
| 61 | + end |
| 62 | + end, |
| 63 | +}) |
| 64 | + |
4 | 65 | mcphub.add_tool("mcphub", { |
5 | 66 | name = "toggle_mcp_server", |
6 | 67 | description = "Start or stop an MCP server. You can only start a server from one of the disabled servers.", |
|
0 commit comments