Skip to content

Commit 9b4d511

Browse files
authored
Merge pull request #218 from ravitemer/feat/copilot-chat
feat: add CopilotChat extension support
2 parents 0b92eae + 8dde167 commit 9b4d511

File tree

11 files changed

+423
-139
lines changed

11 files changed

+423
-139
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ MCP Hub is a MCP client for neovim that seamlessly integrates [MCP (Model Contex
6262
| **Chat Integration** ||||
6363
| | [Avante.nvim](https://github.com/yetone/avante.nvim) || Tools, resources, resourceTemplates, prompts(as slash_commands) |
6464
| | [CodeCompanion.nvim](https://github.com/olimorris/codecompanion.nvim) || Tools, resources, templates, prompts (as slash_commands), 🖼 image responses |
65-
| | [CopilotChat.nvim](https://github.com/CopilotC-Nvim/CopilotChat.nvim) || In-built support [Draft](https://github.com/CopilotC-Nvim/CopilotChat.nvim/pull/1029) |
65+
| | [CopilotChat.nvim](https://github.com/CopilotC-Nvim/CopilotChat.nvim) || Tools, resources, function calling support |
6666
| **Marketplace** ||||
6767
| | Server Discovery || Browse from verified MCP servers |
6868
| | Installation || Manual and auto install with AI |

doc/extensions/copilotchat.md

Lines changed: 50 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,70 @@
1-
# CopilotChat Integration <Badge type="warning" text="Draft"/>
1+
# CopilotChat Integration
22

3-
[CopilotChat.nvim](https://github.com/CopilotC-Nvim/CopilotChat.nvim) supports function calling which is currently in [draft](https://github.com/CopilotC-Nvim/CopilotChat.nvim/pull/1029). To integrate MCP Hub with CopilotChat we need to use the `tools` branch of CopilotChat as shown below:
3+
Add MCP capabilities to [CopilotChat.nvim](https://github.com/CopilotC-Nvim/CopilotChat.nvim) by adding it as an extension. CopilotChat now has native function-calling support, making it easy to integrate MCP tools and resources.
44

5-
> [!WARNING]
6-
> Please note that CopilotChat function-calling support is available as a [Draft PR](https://github.com/CopilotC-Nvim/CopilotChat.nvim/pull/1029).
5+
## Features
76

8-
## Install CopilotChat
7+
- **Tool Integration**: Register MCP tools as CopilotChat functions with proper schemas
8+
- **Resource Integration**: Register MCP resources as CopilotChat functions for easy access
9+
- **Server Groups**: Functions are organized by MCP server name for better organization
10+
- **Real-time Updates**: Automatic updates in CopilotChat when MCP servers change
911

10-
```lua
11-
{
12-
"deathbeam/CopilotChat.nvim",
13-
dependencies = {
14-
{ "zbirenbaum/copilot.lua" },
15-
{ "nvim-lua/plenary.nvim", branch = "master" }, -- for curl, log and async functions
16-
},
17-
branch = "tools",
18-
build = "make tiktoken", -- Only on MacOS or Linux
19-
}
20-
```
12+
## Setup
2113

22-
## Integrate MCP Hub
14+
#### Enable CopilotChat Extension
2315

24-
After the `setup()` of CopilotChat is called, add the following code. Please see the [draft PR](https://github.com/CopilotC-Nvim/CopilotChat.nvim/pull/1029) for more information.
16+
Add CopilotChat as an extension in your MCPHub configuration:
2517

2618
```lua
27-
local chat = require("CopilotChat")
28-
chat.setup()
29-
30-
local mcp = require("mcphub")
31-
mcp.on({ "servers_updated", "tool_list_changed", "resource_list_changed" }, function()
32-
local hub = mcp.get_hub_instance()
33-
if not hub then
34-
return
35-
end
36-
37-
local async = require("plenary.async")
38-
local call_tool = async.wrap(function(server, tool, input, callback)
39-
hub:call_tool(server, tool, input, {
40-
callback = function(res, err)
41-
callback(res, err)
42-
end,
43-
})
44-
end, 4)
45-
46-
local access_resource = async.wrap(function(server, uri, callback)
47-
hub:access_resource(server, uri, {
48-
callback = function(res, err)
49-
callback(res, err)
50-
end,
51-
})
52-
end, 3)
53-
54-
for name, tool in pairs(chat.config.functions) do
55-
if tool.id and tool.id:sub(1, 3) == "mcp" then
56-
chat.config.functions[name] = nil
57-
end
58-
end
59-
local resources = hub:get_resources()
60-
for _, resource in ipairs(resources) do
61-
local name = resource.name:lower():gsub(" ", "_"):gsub(":", "")
62-
chat.config.functions[name] = {
63-
id = "mcp:" .. resource.server_name .. ":" .. name,
64-
uri = resource.uri,
65-
description = type(resource.description) == "string" and resource.description or "",
66-
resolve = function()
67-
local res, err = access_resource(resource.server_name, resource.uri)
68-
if err then
69-
error(err)
70-
end
71-
72-
res = res or {}
73-
local result = res.result or {}
74-
local content = result.contents or {}
75-
local out = {}
76-
77-
for _, message in ipairs(content) do
78-
if message.text then
79-
table.insert(out, {
80-
uri = message.uri,
81-
data = message.text,
82-
mimetype = message.mimeType,
83-
})
84-
end
85-
end
86-
87-
return out
88-
end,
89-
}
90-
end
91-
92-
local tools = hub:get_tools()
93-
for _, tool in ipairs(tools) do
94-
chat.config.functions[tool.name] = {
95-
id = "mcp:" .. tool.server_name .. ":" .. tool.name,
96-
group = tool.server_name,
97-
description = tool.description,
98-
schema = tool.inputSchema,
99-
resolve = function(input)
100-
local res, err = call_tool(tool.server_name, tool.name, input)
101-
if err then
102-
error(err)
103-
end
104-
105-
res = res or {}
106-
local result = res.result or {}
107-
local content = result.content or {}
108-
local out = {}
109-
110-
for _, message in ipairs(content) do
111-
if message.type == "text" then
112-
table.insert(out, {
113-
data = message.text,
114-
})
115-
elseif message.type == "resource" and message.resource and message.resource.text then
116-
table.insert(out, {
117-
uri = message.resource.uri,
118-
data = message.resource.text,
119-
mimetype = message.resource.mimeType,
120-
})
121-
end
122-
end
123-
124-
return out
125-
end,
126-
}
127-
end
128-
end)
19+
require("mcphub").setup({
20+
extensions = {
21+
copilotchat = {
22+
enabled = true,
23+
convert_tools_to_functions = true, -- Convert MCP tools to CopilotChat functions
24+
convert_resources_to_functions = true, -- Convert MCP resources to CopilotChat functions
25+
add_mcp_prefix = false, -- Add "mcp_" prefix to function names
26+
}
27+
}
28+
})
12929
```
13030

31+
#### Configuration Options
32+
33+
- **`convert_tools_to_functions`**: When `true`, all MCP tools are registered as CopilotChat functions
34+
- **`convert_resources_to_functions`**: When `true`, all MCP resources are registered as CopilotChat functions
35+
- **`add_mcp_prefix`**: When `true`, adds "mcp_" prefix to all function names (e.g., `mcp_github__get_issue`)
36+
13137
## Usage
13238

133-
#### MCP Servers As Tools
39+
### MCP Tools
40+
41+
When `convert_tools_to_functions = true`, all MCP tools become available as CopilotChat functions. Functions are automatically organized by server name, making it easy to see all tools from a specific MCP server. Tool names follow the pattern `server_name__tool_name`:
42+
43+
**Examples:**
44+
- `@neovim__read_file` - Read a file using the neovim server
45+
- `@github__create_issue` - Create a GitHub issue
46+
- `@fetch__fetch` - Fetch web content
47+
48+
![Tool Functions](https://github.com/user-attachments/assets/7c16bc7e-a9df-4afc-9736-2ee6a39919a9)
49+
50+
### MCP Resources
13451

135-
You can type `@` in the chat to see all the available tools in CopilotChat. CopilotChat allows us to add all the tools of a MCP server as a tool group as well as the individual tools. For e.g `> @neovim` will add all the tools of Neovim MCP server to the chat. The `>` at the start makes it sticky which means the tools will be sent with all user prompts. You can also just add a specific tool from Neovim server by selecting from the group.
52+
You can use `#` to access MCP resources as variables in CopilotChat. In addition, when `convert_resources_to_functions = true`, all MCP resources will also be available as CopilotChat functions:
13653

137-
![Image](https://github.com/user-attachments/assets/7c16bc7e-a9df-4afc-9736-2ee6a39919a9)
54+
**Examples:**
55+
- `#neovim__Buffer` - Access current buffer content
56+
- `@neovim__Buffer` - Get current buffer content (as a function)
13857

139-
![Image](https://github.com/user-attachments/assets/adc556bb-7d5f-4d22-820a-a7daeb0ac72c)
58+
![Resource Functions](https://github.com/user-attachments/assets/7f77bf1e-12b7-4745-a87b-40181a619733)
14059

141-
#### MCP Resources As Variables
14260

143-
Resources from MCP servers will also be available as CopilotChat variables `#`.
14461

145-
![Image](https://github.com/user-attachments/assets/7f77bf1e-12b7-4745-a87b-40181a619733)
62+
## Example Workflow
14663

64+
1. **Start CopilotChat** and type `@` to see available functions
65+
2. **Select MCP functions** from your connected servers
66+
3. **Use tools**: `@github__create_issue Fix the navigation bug`
67+
4. **Access resources**: `@neovim__current_buffer Show me the current file content`
68+
5. **Organize by server**: Browse functions grouped by MCP server
14769

70+
![Server Groups](https://github.com/user-attachments/assets/adc556bb-7d5f-4d22-820a-a7daeb0ac72c)

doc/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ Users can view all active workspace hubs and switch between them seamlessly thro
138138
| **Chat Integration** ||||
139139
| | [Avante.nvim](https://github.com/yetone/avante.nvim) || Tools, resources, resourceTemplates, prompts(as slash_commands) |
140140
| | [CodeCompanion.nvim](https://github.com/olimorris/codecompanion.nvim) || Tools, resources, templates, prompts (as slash_commands), 🖼 image responses |
141-
| | [CopilotChat.nvim](https://github.com/CopilotC-Nvim/CopilotChat.nvim) || In-built support [Draft](https://github.com/CopilotC-Nvim/CopilotChat.nvim/pull/1029) |
141+
| | [CopilotChat.nvim](https://github.com/CopilotC-Nvim/CopilotChat.nvim) || Tools, resources, function calling support |
142142
| **Marketplace** ||||
143143
| | Server Discovery || Browse from verified MCP servers |
144144
| | Installation || Manual and auto install with AI |

lua/mcphub/config.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ local defaults = {
8585
enabled = true,
8686
make_slash_commands = true,
8787
},
88+
copilotchat = {
89+
enabled = true,
90+
convert_tools_to_functions = true,
91+
convert_resources_to_functions = true,
92+
add_mcp_prefix = false,
93+
},
8894
},
8995
---@type MCPHub.WorkspaceConfig
9096
workspace = {

0 commit comments

Comments
 (0)