1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//! Where the composer's MCP prompts and resources come from: the servers
//! the session's tools use, which are the daemon's. The TUI reads them from
//! there (`daemon_session`'s `DaemonMcp`).
//!
//! Palette matching is synchronous, so what the session's servers offer is
//! read ahead into [`DaemonMcpOffers`] and matched from there. The read is
//! clock-free, like `mcp_argument_completion`'s: one starts when the
//! composer begins an `@` mention or an MCP prompt command, and one more
//! once the session's tool sources loaded, if the first ran before they had.
use super::{daemon_session::DaemonMcp, file_picker::PathTokenSource, App, ComposerMode};
use crate::{
commands,
tools::mcp::{catalog::McpPrompt, prompt_command, McpCompletionSupport, McpResource},
};
/// The prompts and resources a session's MCP servers offer.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(super) struct McpOffers {
/// Each prompt, with whether its server suggests values for its
/// arguments.
pub(super) prompts: Vec<(McpPrompt, McpCompletionSupport)>,
pub(super) resources: Vec<McpResource>,
}
/// What the session's servers offer, as last read.
#[derive(Debug, Default)]
pub(super) struct DaemonMcpOffers {
offers: McpOffers,
/// The one read in flight; `None` from it when the daemon did not say.
pending: Option<tokio::task::JoinHandle<Option<McpOffers>>>,
/// The read the composer's current mention or command started; `None`
/// while the composer shows neither.
asked: Option<Asked>,
}
/// Whether the session's tool sources still loaded when a read started,
/// which says whether the servers may offer more once they have.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Asked {
WhileLoading,
Loaded,
}
impl DaemonMcpOffers {
pub(super) fn is_pending(&self) -> bool {
self.pending.is_some()
}
}
impl App {
/// A handle that fetches from the session's MCP servers, for a task
/// that outlives a borrow of the app.
pub(super) fn mcp_source(&self) -> DaemonMcp {
self.info.session.daemon.mcp()
}
/// Every resource the session's servers offer, for `@` matching.
pub(super) fn mcp_resources(&self) -> Vec<McpResource> {
self.daemon_mcp_offers.offers.resources.clone()
}
/// The prompt `server` offers as `name`, and whether that server
/// suggests values for its arguments.
pub(super) fn mcp_prompt(
&self,
server: &str,
name: &str,
) -> Option<(McpPrompt, McpCompletionSupport)> {
self.daemon_mcp_offers
.offers
.prompts
.iter()
.find(|(prompt, _)| prompt.server == server && prompt.name == name)
.cloned()
}
/// Forget what the session left behind offered.
pub(super) fn reset_daemon_mcp_offers(&mut self) {
self.daemon_mcp_offers = DaemonMcpOffers::default();
}
/// Collect a finished read of what the session's servers offer and
/// start the next one the composer calls for. Returns whether the
/// palettes must redraw.
pub(super) async fn poll_daemon_mcp_offers(&mut self) -> bool {
let session = &self.info.session.daemon;
let mut redraw = false;
if let Some(pending) = self
.daemon_mcp_offers
.pending
.take_if(|pending| pending.is_finished())
{
// A read the daemon did not answer keeps what was offered before.
if let Ok(Some(offers)) = pending.await {
if offers != self.daemon_mcp_offers.offers {
self.daemon_mcp_offers.offers = offers;
// Matches cached for the mention being typed predate it.
self.palette_caches.clear_file();
redraw = true;
}
}
}
let text = self.input_ui.text();
let reads_mcp = matches!(self.input_ui.composer(), ComposerMode::Input)
&& (self
.active_path_token()
.is_some_and(|token| token.source == PathTokenSource::Mention)
|| commands::command_prefix(text)
.and_then(prompt_command::parse_command)
.is_some());
if !reads_mcp {
self.daemon_mcp_offers.asked = None;
return redraw;
}
let loading = session.tool_sources_loading();
let stale = match self.daemon_mcp_offers.asked {
None => true,
Some(Asked::WhileLoading) => !loading,
Some(Asked::Loaded) => false,
};
if stale && self.daemon_mcp_offers.pending.is_none() {
self.daemon_mcp_offers.asked = Some(if loading {
Asked::WhileLoading
} else {
Asked::Loaded
});
self.daemon_mcp_offers.pending = Some(tokio::spawn(session.mcp().offers()));
}
redraw
}
}