Skip to content

Commit c3413f9

Browse files
committed
fix(api): mcp error with remote server
Fixes #93
1 parent ad1260e commit c3413f9

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

lua/opencode/api.lua

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ function M.mcp()
474474
local info = require('opencode.config_file')
475475
local mcp = info.get_mcp_servers()
476476
if not mcp then
477-
ui.notify('No MCP configuration found. Please check your opencode config file.', 'warn')
477+
vim.notify('No MCP configuration found. Please check your opencode config file.', vim.log.levels.WARN)
478478
return
479479
end
480480

@@ -484,19 +484,26 @@ function M.mcp()
484484
local msg = M.with_header({
485485
'### Available MCP servers',
486486
'',
487-
'| Name | Type | cmd |',
488-
'|--------|------|-----|',
487+
'| Name | Type | cmd/url |',
488+
'|--------|------|---------|',
489489
})
490490

491491
for name, def in pairs(mcp) do
492+
local cmd_or_url
493+
if def.type == 'local' then
494+
cmd_or_url = def.command and table.concat(def.command, ' ')
495+
elseif def.type == 'remote' then
496+
cmd_or_url = def.url
497+
end
498+
492499
table.insert(
493500
msg,
494501
string.format(
495502
'| %s %-10s | %s | %s |',
496503
(def.enabled and icons.get('status_on') or icons.get('status_off')),
497504
name,
498505
def.type,
499-
table.concat(def.command, ' ')
506+
cmd_or_url
500507
)
501508
)
502509
end

tests/unit/api_spec.lua

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,66 @@ describe('opencode.api', function()
214214
})
215215
end)
216216
end)
217+
218+
describe('/mcp command', function()
219+
it('displays MCP server configuration when available', function()
220+
local config_file = require('opencode.config_file')
221+
local original_get_mcp_servers = config_file.get_mcp_servers
222+
223+
config_file.get_mcp_servers = function()
224+
return {
225+
filesystem = {
226+
type = 'local',
227+
enabled = true,
228+
command = { 'npx', '-y', '@modelcontextprotocol/server-filesystem' },
229+
},
230+
github = {
231+
type = 'remote',
232+
enabled = false,
233+
url = 'https://example.com/mcp',
234+
},
235+
}
236+
end
237+
238+
stub(ui, 'render_lines')
239+
stub(api, 'open_input')
240+
241+
api.mcp()
242+
243+
assert.stub(api.open_input).was_called()
244+
assert.stub(ui.render_lines).was_called()
245+
246+
local render_args = ui.render_lines.calls[1].refs[1]
247+
local rendered_text = table.concat(render_args, '\n')
248+
249+
assert.truthy(rendered_text:match('Available MCP servers'))
250+
assert.truthy(rendered_text:match('filesystem'))
251+
assert.truthy(rendered_text:match('github'))
252+
assert.truthy(rendered_text:match('local'))
253+
assert.truthy(rendered_text:match('remote'))
254+
255+
config_file.get_mcp_servers = original_get_mcp_servers
256+
end)
257+
258+
it('shows warning when no MCP configuration exists', function()
259+
local config_file = require('opencode.config_file')
260+
local original_get_mcp_servers = config_file.get_mcp_servers
261+
262+
config_file.get_mcp_servers = function()
263+
return nil
264+
end
265+
266+
local notify_stub = stub(vim, 'notify')
267+
268+
api.mcp()
269+
270+
assert.stub(notify_stub).was_called_with(
271+
'No MCP configuration found. Please check your opencode config file.',
272+
vim.log.levels.WARN
273+
)
274+
275+
config_file.get_mcp_servers = original_get_mcp_servers
276+
notify_stub:revert()
277+
end)
278+
end)
217279
end)

0 commit comments

Comments
 (0)