Skip to content

Commit e0b6292

Browse files
authored
feat(extensions): Cleanup active servers prompt markdown (#195)
* chore: only one H1 in @mcp prompt * chore: remove spaces at EOL * chore: remove space at EOL in @mcp prompt * chore: fix Input Schema indent and add code block in @mcp prompt * chore: fix header trailing punctuation in @mcp prompt * chore: fix indent for multiline description in @mcp prompt * chore: add empty line before server description in @mcp prompt * chore: fix extra empty lines in examples in @mcp prompt
1 parent 68c510e commit e0b6292

1 file changed

Lines changed: 25 additions & 23 deletions

File tree

lua/mcphub/utils/prompt.lua

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ local function format_custom_instructions(server_name)
2727
local custom_instructions = server_config.custom_instructions or {}
2828

2929
if custom_instructions.text and custom_instructions.text ~= "" and not custom_instructions.disabled then
30-
return string.format("\n\n### Instructions for `%s` server\n\n" .. custom_instructions.text, server_name)
30+
return string.format("\n\n#### Instructions for `%s` server\n\n" .. custom_instructions.text, server_name)
3131
end
3232
return ""
3333
end
@@ -85,11 +85,14 @@ local function format_tools(tools)
8585
return ""
8686
end
8787

88-
local result = "\n\n### Available Tools"
88+
local result = "\n\n#### Available Tools"
8989
for _, tool in ipairs(tools) do
90-
result = result .. string.format("\n\n- %s: %s", tool.name, M.get_description(tool))
90+
result = result .. string.format("\n\n- %s: %s", tool.name, M.get_description(tool):gsub("\n", "\n "))
9191
local inputSchema = M.get_inputSchema(tool)
92-
result = result .. "\n Input Schema:\n " .. vim.inspect(inputSchema):gsub("\n", "\n ")
92+
result = result
93+
.. "\n\n Input Schema:\n\n ```lua\n "
94+
.. vim.inspect(inputSchema):gsub("\n", "\n ")
95+
.. "\n ```"
9396
end
9497
return result
9598
end
@@ -114,21 +117,21 @@ end
114117
local function format_resources(resources, templates)
115118
local result = ""
116119
if resources and #resources > 0 then
117-
result = result .. "\n\n### Available Resources"
120+
result = result .. "\n\n#### Available Resources"
118121
for _, resource in ipairs(resources) do
119122
result = result
120123
.. string.format(
121124
"\n\n- %s%s",
122125
resource.uri,
123126
resource.mimeType and " (" .. resource.mimeType .. ")" or ""
124127
)
125-
local desc = M.get_description(resource)
128+
local desc = M.get_description(resource):gsub("\n", "\n ")
126129
result = result .. "\n " .. (resource.name or "") .. (desc == "" and "" or "\n " .. desc)
127130
-- result = result .. "\n\n" .. vim.inspect(remove_functions(resource))
128131
end
129132
end
130133
if templates and #templates > 0 then
131-
result = result .. "\n\n### Available Resource Templates"
134+
result = result .. "\n\n#### Available Resource Templates"
132135
for _, template in ipairs(templates) do
133136
result = result .. string.format("\n\n- %s", template.uriTemplate)
134137
local desc = M.get_description(template)
@@ -156,7 +159,7 @@ function M.get_use_mcp_tool_prompt(example)
156159

157160
return string.format(
158161
[[
159-
## use_mcp_tool
162+
### use_mcp_tool
160163
161164
Description: Request to use a tool provided by a connected MCP server. Each MCP server can provide multiple tools with different capabilities. Tools have defined input schemas that specify required and optional parameters.
162165
Parameters:
@@ -182,7 +185,7 @@ function M.get_access_mcp_resource_prompt(example)
182185

183186
return string.format(
184187
[[
185-
## access_mcp_resource
188+
### access_mcp_resource
186189
187190
Description: Request to access a resource provided by a connected MCP server. Resources represent data sources that can be used as context, such as files, API responses, or system information.
188191
Parameters:
@@ -201,14 +204,14 @@ end
201204
function M.server_to_text(server)
202205
local text = ""
203206
-- Add server section
204-
text = text .. string.format("## %s", server.name)
207+
text = text .. string.format("### %s", server.name)
205208
local is_disabled = server.status == "disabled"
206209
if is_disabled then
207210
text = text .. " (Disabled)"
208211
end
209212
local desc = M.get_description(server)
210213
-- Add description
211-
text = text .. (desc == "" and "" or "\n" .. desc)
214+
text = text .. (desc == "" and "" or "\n\n" .. desc)
212215
if is_disabled then
213216
return text
214217
end
@@ -251,11 +254,11 @@ function M.get_active_servers_prompt(servers, add_example, enable_toggling_mcp_s
251254
return s.status == "disabled"
252255
end, servers)
253256

254-
prompt = prompt .. "\n# Connected MCP Servers"
257+
prompt = prompt .. "\n## Connected MCP Servers"
255258

256259
prompt = prompt
257260
.. "\n\nWhen a server is connected, you can use the server's tools via the `use_mcp_tool` tool, "
258-
.. "and access the server's resources via the `access_mcp_resource` tool.\nNote: Server names are case sensitive and you should always use the exact full name like `Firecrawl MCP` or `src/user/main/time-mcp` etc \n\n"
261+
.. "and access the server's resources via the `access_mcp_resource` tool.\nNote: Server names are case sensitive and you should always use the exact full name like `Firecrawl MCP` or `src/user/main/time-mcp` etc\n\n"
259262
if #connected_servers == 0 then
260263
prompt = prompt .. "(No connected MCP servers)\n\n"
261264
else
@@ -265,7 +268,7 @@ function M.get_active_servers_prompt(servers, add_example, enable_toggling_mcp_s
265268
end
266269

267270
-- instead of removing the whole disabled section, if we dont want auto toggling we set (NO disabled servers) to avoid llm hallucinating server names
268-
prompt = prompt .. "# Disabled MCP Servers\n\n"
271+
prompt = prompt .. "## Disabled MCP Servers\n\n"
269272
prompt = prompt
270273
.. "When a server is disabled, it will not be able to provide tools or resources. You can start one of the following disabled servers by using the `toggle_mcp_server` tool on `mcphub` MCP Server if it is connected using `use_mcp_tool`\n\n"
271274
if not enable_toggling_mcp_servers or #disabled_servers == 0 then
@@ -275,7 +278,9 @@ function M.get_active_servers_prompt(servers, add_example, enable_toggling_mcp_s
275278
prompt = prompt .. M.server_to_text(server) .. "\n\n"
276279
end
277280
end
278-
local toggle_example = [[## Toggling a MCP Server
281+
local toggle_example = [[
282+
283+
### Toggling a MCP Server
279284
280285
When you need to start a disabled MCP Server or vice-versa, use the `toggle_mcp_server` tool on `mcphub` MCP Server using `use_mcp_tool`:
281286
@@ -286,17 +291,15 @@ Pseudocode:
286291
use_mcp_tool
287292
server_name: "mcphub"
288293
tool_name: "toggle_mcp_server"
289-
tool_input:
294+
tool_input:
290295
server_name: string (One of the available server names to start or stop)
291296
action: string (one of `start` or `stop`)
292297
]]
293298

294299
local example = [[
300+
## Examples
295301
296-
297-
# Examples:
298-
299-
## `use_mcp_tool`
302+
### `use_mcp_tool`
300303
301304
When you need to call a tool on an MCP Server, use the `use_mcp_tool` tool:
302305
@@ -307,7 +310,7 @@ use_mcp_tool
307310
tool_name: string (name of the tool in the server to call)
308311
tool_input: object (Arguments for the tool call)
309312
310-
## `access_mcp_resource`
313+
### `access_mcp_resource`
311314
312315
When you need to access a resource from a MCP Server, use the `access_mcp_resource` tool:
313316
@@ -316,7 +319,6 @@ Pseudocode:
316319
access_mcp_resource
317320
server_name: string (One of the available server names)
318321
uri: string (uri for the resource)
319-
320322
]]
321323

322324
if enable_toggling_mcp_servers then
@@ -534,7 +536,7 @@ Server Details:
534536
535537
Installation Instructions:
536538
1. Review README instructions below
537-
2. Follow setup steps from README
539+
2. Follow setup steps from README
538540
3. Ask the user to provide any env variables or details required for the server configuration.
539541
4. Update config at %s
540542
5. Ask the user to Restart MCPHub by opening the UI and pressing "R" to restart the hub with updated config and ask the user to provide you with mcp tool if not already provided.

0 commit comments

Comments
 (0)