Skip to content

Commit 5491de9

Browse files
authored
feat(prompt_library): prompts can contain tools and MCP servers (#2725)
1 parent a8cbf87 commit 5491de9

File tree

17 files changed

+651
-42
lines changed

17 files changed

+651
-42
lines changed

doc/.vitepress/config.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ export default withMermaid(
170170
},
171171
{ text: "Events", link: "/usage/events" },
172172
{ text: "Inline Assistant", link: "/usage/inline-assistant" },
173-
{ text: "Action Palette", link: "/usage/action-palette" },
174173
{ text: "Prompt Library", link: "/usage/prompt-library" },
175174
{ text: "Workflows", link: "/usage/workflows" },
176175
{ text: "UI", link: "/usage/ui" },

doc/codecompanion.txt

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*codecompanion.txt* For NVIM v0.11 Last change: 2026 February 06
1+
*codecompanion.txt* For NVIM v0.11 Last change: 2026 February 08
22

33
==============================================================================
44
Table of Contents *codecompanion-table-of-contents*
@@ -562,6 +562,21 @@ CONFIG ~
562562
- All diff config has moved to `display.diff` (#2600 <https://github.com/olimorris/codecompanion.nvim/pull/2600>)
563563

564564

565+
PROMPT LIBRARY ~
566+
567+
- The location of rules within a prompt library item has changed from `opts.rules` to `rules`. They now also support workflows:
568+
569+
>markdown
570+
---
571+
name: Oli's test workflow
572+
strategy: chat
573+
description: Workflow test prompt
574+
rules:
575+
- test_rule
576+
---
577+
<
578+
579+
565580
V17.33.0 TO V18.0.0 *codecompanion-upgrading-general-v17.33.0-to-v18.0.0*
566581

567582

@@ -2450,6 +2465,10 @@ Palette - `description` - Description shown in the Action Palette -
24502465
**Prompt sections:** - `## system` - System messages that set the LLM’s
24512466
behaviour - `## user` - User messages containing your requests
24522467

2468+
In the markdown prompt, above,
2469+
|codecompanion-configuration-prompt-library-with-placeholders| are used to
2470+
inject dynamic content from a visual selection.
2471+
24532472

24542473
OPTIONS
24552474

@@ -2471,12 +2490,11 @@ behaviour:
24712490
- `modes` - Only show in specific modes (`{ "v" }` for visual mode)
24722491
- `placement` - For inline interaction: `new`, `replace`, `add`, `before`, `chat`
24732492
- `pre_hook` - Function to run before the prompt is executed (Lua only)
2474-
- `rules` - Specify a rule group to load with the prompt
24752493
- `stop_context_insertion` - Prevent automatic context insertion
24762494
- `user_prompt` - Get user input before actioning the response
24772495

24782496

2479-
PLACEHOLDERS
2497+
WITH PLACEHOLDERS
24802498

24812499
Placeholders allow you to inject dynamic content into your prompts. In markdown
24822500
prompts, use `${placeholder.name}` syntax:
@@ -2640,6 +2658,13 @@ Context items appear at the top of the chat buffer. URLs are automatically
26402658
cached for you.
26412659

26422660

2661+
MCP SERVERS
2662+
2663+
You can also specify |codecompanion-configuration-mcp| to be loaded with your
2664+
prompt:
2665+
2666+
2667+
26432668
PICKERS
26442669

26452670
Pickers allow you to create dynamic prompt menus based on runtime data.
@@ -2713,6 +2738,19 @@ the `pre_hook` and assume that is the buffer number you wish any code to be
27132738
streamed into.
27142739

27152740

2741+
RULES
2742+
2743+
You can also specify rules to be loaded with your prompt:
2744+
2745+
2746+
2747+
TOOLS
2748+
2749+
You can also specify tools to be loaded with your prompt. These can be
2750+
individual tools as well as tool groups:
2751+
2752+
2753+
27162754
WORKFLOWS
27172755

27182756
Workflows allow you to chain multiple prompts together in a sequence. That is,
@@ -4505,6 +4543,13 @@ function:
45054543
Where `docs` is the `alias` of the prompt.
45064544

45074545

4546+
SLASH COMMANDS ~
4547+
4548+
If your prompt library entries have an `alias` defined then you can invoke them
4549+
using a slash command. In the cmd line `:CodeCompanion /<alias>` or `/<alias>`
4550+
if you’re in the chat buffer.
4551+
4552+
45084553
USER INTERFACE *codecompanion-usage-user-interface*
45094554

45104555
CodeCompanion aims to keep any changes to the user’s UI to a minimum.

doc/configuration/prompt-library.md

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ Markdown prompts consist of two main parts:
147147
- `## system` - System messages that set the LLM's behaviour
148148
- `## user` - User messages containing your requests
149149

150+
In the markdown prompt, above, [placeholders](/configuration/prompt-library#with-placeholders) are used to inject dynamic content from a visual selection.
151+
150152
### Options
151153

152154
Both markdown and Lua prompts support a wide range of options to customise behaviour:
@@ -241,11 +243,10 @@ opts = {
241243
- `modes` - Only show in specific modes (`{ "v" }` for visual mode)
242244
- `placement` - For inline interaction: `new`, `replace`, `add`, `before`, `chat`
243245
- `pre_hook` - Function to run before the prompt is executed (Lua only)
244-
- `rules` - Specify a rule group to load with the prompt
245246
- `stop_context_insertion` - Prevent automatic context insertion
246247
- `user_prompt` - Get user input before actioning the response
247248

248-
### Placeholders
249+
### With Placeholders
249250

250251
Placeholders allow you to inject dynamic content into your prompts. In markdown prompts, use `${placeholder.name}` syntax:
251252

@@ -515,6 +516,36 @@ I'll think of something clever to put here...
515516

516517
Context items appear at the top of the chat buffer. URLs are automatically cached for you.
517518

519+
#### MCP Servers
520+
521+
You can also specify [MCP servers](/configuration/mcp) to be loaded with your prompt:
522+
523+
::: code-group
524+
525+
````markdown [Markdown]
526+
---
527+
name: Prompt with MCP servers
528+
interaction: chat
529+
description: A prompt that starts MCP servers
530+
mcp_servers:
531+
- tavily-mcp
532+
- filesystem
533+
---
534+
````
535+
536+
````lua [Lua]
537+
["Prompt with MCP servers"] = {
538+
interaction = "chat",
539+
description = "A prompt that starts MCP servers",
540+
mcp_servers = {
541+
"tavily-mcp",
542+
"filesystem",
543+
},
544+
},
545+
````
546+
547+
:::
548+
518549
#### Pickers
519550

520551
Pickers allow you to create dynamic prompt menus based on runtime data.
@@ -583,6 +614,66 @@ Pre-hooks allow you to run custom logic before a prompt is executed. This is par
583614

584615
For the inline interaction, the plugin will detect a number being returned from the `pre_hook` and assume that is the buffer number you wish any code to be streamed into.
585616

617+
#### Rules
618+
619+
You can also specify rules to be loaded with your prompt:
620+
621+
::: code-group
622+
623+
````markdown [Markdown]
624+
---
625+
name: Prompt with rules
626+
interaction: chat
627+
description: A prompt that loads rules
628+
rules:
629+
- default
630+
- my_other_rule
631+
---
632+
````
633+
634+
````lua [Lua]
635+
["Prompt with rules"] = {
636+
interaction = "chat",
637+
description = "A prompt that loads rules",
638+
rules = {
639+
"default",
640+
"my_other_rules",
641+
},
642+
},
643+
````
644+
645+
:::
646+
647+
#### Tools
648+
649+
You can also specify tools to be loaded with your prompt. These can be individual tools as well as tool groups:
650+
651+
::: code-group
652+
653+
````markdown [Markdown]
654+
---
655+
name: Prompt with tools
656+
interaction: chat
657+
description: A prompt that loads tools
658+
tools:
659+
- cmd_runner
660+
- insert_edit_into_file
661+
---
662+
````
663+
664+
````lua [Lua]
665+
["Prompt with tools"] = {
666+
interaction = "chat",
667+
description = "A prompt that loads tools",
668+
tools = {
669+
"cmd_runner",
670+
"insert_edit_into_file",
671+
},
672+
},
673+
````
674+
675+
:::
676+
586677
#### Workflows
587678

588679
Workflows allow you to chain multiple prompts together in a sequence. That is, the first prompt is sent to the LLM, the LLM responds, then the next prompt in the workflow is sent, etc. This can be useful for implementing multi-step processes such as chain-of-thought reasoning or iterative code refinement.

doc/upgrading.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ CodeCompanion follows [semantic versioning](https://semver.org/) and to avoid br
1818
- Diff keymaps have moved from `interactions.inline.keymaps` to `interactions.shared.keymaps` ([#2600](https://github.com/olimorris/codecompanion.nvim/pull/2600))
1919
- All diff config has moved to `display.diff` ([#2600](https://github.com/olimorris/codecompanion.nvim/pull/2600))
2020

21+
### Prompt Library
22+
23+
- The location of rules within a prompt library item has changed from `opts.rules` to `rules`. They now also support workflows:
24+
25+
```markdown
26+
---
27+
name: Oli's test workflow
28+
strategy: chat
29+
description: Workflow test prompt
30+
rules:
31+
- test_rule
32+
---
33+
```
34+
2135
## v17.33.0 to v18.0.0
2236

2337
### Config

doc/usage/prompt-library.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ end, { noremap = true, silent = true })
1818

1919
Where `docs` is the `alias` of the prompt.
2020

21+
## Slash Commands
22+
23+
If your prompt library entries have an `alias` defined then you can invoke them using a slash command. In the cmd line `:CodeCompanion /<alias>` or `/<alias>` if you're in the chat buffer.
24+

lua/codecompanion/actions/markdown.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,13 @@ function M.parse_file(path, context)
6464
context = frontmatter.context or nil,
6565
description = frontmatter.description or "",
6666
interaction = frontmatter.interaction,
67+
mcp_servers = frontmatter.mcp_servers,
6768
name = frontmatter.name,
6869
opts = frontmatter.opts or {},
6970
path = path,
7071
prompts = prompts,
72+
rules = frontmatter.rules,
73+
tools = frontmatter.tools,
7174
}
7275
end
7376

@@ -127,7 +130,7 @@ function M.parse_frontmatter(content)
127130
end
128131
end
129132

130-
--TODO: Remove in v19.0.0
133+
--TODO: Remove in v20.0.0
131134
if frontmatter.strategy then
132135
frontmatter.interaction = frontmatter.strategy
133136
frontmatter.strategy = nil

lua/codecompanion/actions/prompt_library.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ function M.resolve(context, config)
3535
context = prompt.context,
3636
description = description,
3737
interaction = prompt.interaction or prompt.strategy,
38+
mcp_servers = prompt.mcp_servers,
3839
name = name,
3940
opts = prompt.opts,
4041
picker = prompt.picker,
4142
prompts = prompt.prompts,
43+
rules = prompt.rules,
44+
tools = prompt.tools,
4245
})
4346

4447
::continue::

lua/codecompanion/interactions/chat/helpers/init.lua

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
local config = require("codecompanion.config")
2+
13
local Path = require("plenary.path")
24
local buf_utils = require("codecompanion.utils.buffers")
3-
local config = require("codecompanion.config")
5+
local log = require("codecompanion.utils.log")
46

57
local M = {}
68

@@ -89,6 +91,37 @@ function M.has_tag(tag, messages)
8991
)
9092
end
9193

94+
---Start MCP servers and add their tools to the chat buffer
95+
---@param chat CodeCompanion.Chat
96+
---@param server_names table<string> List of MCP server names
97+
---@return nil
98+
function M.start_mcp_servers(chat, server_names)
99+
local mcp = require("codecompanion.mcp")
100+
101+
---Add an MCP server's tool group to the chat buffer
102+
---@param name string
103+
local function add_tools(name)
104+
chat.tools:refresh({ adapter = chat.adapter })
105+
chat.tool_registry:add(mcp.tool_prefix() .. name, { config = chat.tools.tools_config })
106+
log:debug("Added MCP server tools for `%s` to chat %d", name, chat.id)
107+
end
108+
109+
for _, name in ipairs(server_names) do
110+
local status = mcp.get_status()
111+
local server_status = status[name]
112+
113+
if server_status and server_status.ready and server_status.tool_count > 0 then
114+
add_tools(name)
115+
else
116+
mcp.enable_server(name, {
117+
on_tools_loaded = function()
118+
add_tools(name)
119+
end,
120+
})
121+
end
122+
end
123+
end
124+
92125
---Determine if context has already been added to the messages stack
93126
---@param context string
94127
---@param messages CodeCompanion.Chat.Messages

0 commit comments

Comments
 (0)