Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions lua/opencode/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ function M.mcp()
local info = require('opencode.config_file')
local mcp = info.get_mcp_servers()
if not mcp then
ui.notify('No MCP configuration found. Please check your opencode config file.', 'warn')
vim.notify('No MCP configuration found. Please check your opencode config file.', vim.log.levels.WARN)
return
end

Expand All @@ -484,19 +484,26 @@ function M.mcp()
local msg = M.with_header({
'### Available MCP servers',
'',
'| Name | Type | cmd |',
'|--------|------|-----|',
'| Name | Type | cmd/url |',
'|--------|------|---------|',
})

for name, def in pairs(mcp) do
local cmd_or_url
if def.type == 'local' then
cmd_or_url = def.command and table.concat(def.command, ' ')
elseif def.type == 'remote' then
cmd_or_url = def.url
end

table.insert(
msg,
string.format(
'| %s %-10s | %s | %s |',
(def.enabled and icons.get('status_on') or icons.get('status_off')),
name,
def.type,
table.concat(def.command, ' ')
cmd_or_url
)
)
end
Expand Down
62 changes: 62 additions & 0 deletions tests/unit/api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,66 @@ describe('opencode.api', function()
})
end)
end)

describe('/mcp command', function()
it('displays MCP server configuration when available', function()
local config_file = require('opencode.config_file')
local original_get_mcp_servers = config_file.get_mcp_servers

config_file.get_mcp_servers = function()
return {
filesystem = {
type = 'local',
enabled = true,
command = { 'npx', '-y', '@modelcontextprotocol/server-filesystem' },
},
github = {
type = 'remote',
enabled = false,
url = 'https://example.com/mcp',
},
}
end

stub(ui, 'render_lines')
stub(api, 'open_input')

api.mcp()

assert.stub(api.open_input).was_called()
assert.stub(ui.render_lines).was_called()

local render_args = ui.render_lines.calls[1].refs[1]
local rendered_text = table.concat(render_args, '\n')

assert.truthy(rendered_text:match('Available MCP servers'))
assert.truthy(rendered_text:match('filesystem'))
assert.truthy(rendered_text:match('github'))
assert.truthy(rendered_text:match('local'))
assert.truthy(rendered_text:match('remote'))

config_file.get_mcp_servers = original_get_mcp_servers
end)

it('shows warning when no MCP configuration exists', function()
local config_file = require('opencode.config_file')
local original_get_mcp_servers = config_file.get_mcp_servers

config_file.get_mcp_servers = function()
return nil
end

local notify_stub = stub(vim, 'notify')

api.mcp()

assert.stub(notify_stub).was_called_with(
'No MCP configuration found. Please check your opencode config file.',
vim.log.levels.WARN
)

config_file.get_mcp_servers = original_get_mcp_servers
notify_stub:revert()
end)
end)
end)