@@ -15,6 +15,8 @@ import (
1515 "path/filepath"
1616 "slices"
1717 "strings"
18+ "sync"
19+ "sync/atomic"
1820
1921 "charm.land/catwalk/pkg/catwalk"
2022 "charm.land/fantasy"
@@ -22,6 +24,7 @@ import (
2224 "github.com/charmbracelet/crush/internal/agent/notify"
2325 "github.com/charmbracelet/crush/internal/agent/prompt"
2426 "github.com/charmbracelet/crush/internal/agent/tools"
27+ "github.com/charmbracelet/crush/internal/agent/tools/mcp"
2528 "github.com/charmbracelet/crush/internal/config"
2629 "github.com/charmbracelet/crush/internal/csync"
2730 "github.com/charmbracelet/crush/internal/discover"
@@ -132,6 +135,24 @@ type coordinator struct {
132135 // credential change misses the cache and rebuilds.
133136 providerClients *csync.Map[string, fantasy.Provider]
134137
138+ // toolsCache avoids rebuilding the tool set on every UpdateModels call.
139+ // The cache is invalidated when the config pointer changes (reload or
140+ // mutation) or when mcpToolsGen is bumped (MCP server connect/disconnect
141+ // or tools/list_changed notification).
142+ toolsCacheMu sync.Mutex
143+ toolsCache []fantasy.AgentTool
144+ toolsCacheCfg *config.Config
145+ toolsCacheMCP uint64
146+ mcpToolsGen atomic.Uint64
147+
148+ // providerOptsCache avoids repeating the 3× JSON marshal + merge +
149+ // unmarshal in getProviderOptions on every turn when the model and
150+ // config have not changed. Keyed by config pointer + model CatwalkCfg
151+ // ID + provider name.
152+ providerOptsCacheMu sync.Mutex
153+ providerOptsCache fantasy.ProviderOptions
154+ providerOptsCacheKey string
155+
135156 readyWg errgroup.Group
136157}
137158
@@ -196,6 +217,17 @@ func NewCoordinator(
196217 }
197218 c.currentAgent = agent
198219 c.agents[config.AgentCoder] = agent
220+
221+ // Subscribe to MCP tool-list changes so the tools cache is invalidated
222+ // when MCP servers connect/disconnect or announce new tools.
223+ go func() {
224+ for ev := range mcp.SubscribeEvents(ctx) {
225+ if ev.Payload.Type == mcp.EventToolsListChanged || ev.Payload.Type == mcp.EventStateChanged {
226+ c.mcpToolsGen.Add(1)
227+ }
228+ }
229+ }()
230+
199231 return c, nil
200232}
201233
@@ -246,7 +278,7 @@ func (c *coordinator) run(ctx context.Context, accept *AcceptedRun, sessionID st
246278 return nil, errModelProviderNotConfigured
247279 }
248280
249− mergedOptions, temp, topP, topK, freqPenalty, presPenalty := mergeCallOptions(model, providerCfg)
281+ mergedOptions, temp, topP, topK, freqPenalty, presPenalty := c.cachedMergeCallOptions(model, providerCfg)
250282
251283 if err := c.refreshTokenIfExpired(ctx, providerCfg); err != nil {
252284 // NOTE(@andreynering): We don't return here because the event handling to ask the user to reauthenticate
@@ -588,6 +620,39 @@ func mergeCallOptions(model Model, cfg config.ProviderConfig) (fantasy.ProviderO
588620 return modelOptions, temp, topP, topK, freqPenalty, presPenalty
589621}
590622
623+// cachedMergeCallOptions wraps mergeCallOptions with a memoization cache.
624+// The provider options JSON merge is expensive (3× marshal + merge +
625+// unmarshal) and the inputs rarely change between turns. The cache key
626+// combines the config pointer identity with the model's catwalk ID and
627+// provider name so any config reload or model switch misses the cache.
628+func (c *coordinator) cachedMergeCallOptions(model Model, cfg config.ProviderConfig) (fantasy.ProviderOptions, *float64, *float64, *int64, *float64, *float64) {
629+ key := fmt.Sprintf("%p|%s|%s", c.cfg.Config(), model.CatwalkCfg.ID, model.ModelCfg.Provider)
630+
631+ c.providerOptsCacheMu.Lock()
632+ if c.providerOptsCacheKey == key {
633+ opts := c.providerOptsCache
634+ c.providerOptsCacheMu.Unlock()
635+ // Recompute the scalar overrides (cheap) since they don't need
636+ // the JSON merge — they're just cmp.Or calls.
637+ temp := cmp.Or(model.ModelCfg.Temperature, model.CatwalkCfg.Options.Temperature)
638+ topP := cmp.Or(model.ModelCfg.TopP, model.CatwalkCfg.Options.TopP)
639+ topK := cmp.Or(model.ModelCfg.TopK, model.CatwalkCfg.Options.TopK)
640+ freqPenalty := cmp.Or(model.ModelCfg.FrequencyPenalty, model.CatwalkCfg.Options.FrequencyPenalty)
641+ presPenalty := cmp.Or(model.ModelCfg.PresencePenalty, model.CatwalkCfg.Options.PresencePenalty)
642+ return opts, temp, topP, topK, freqPenalty, presPenalty
643+ }
644+ c.providerOptsCacheMu.Unlock()
645+
646+ opts, temp, topP, topK, freqPenalty, presPenalty := mergeCallOptions(model, cfg)
647+
648+ c.providerOptsCacheMu.Lock()
649+ c.providerOptsCache = opts
650+ c.providerOptsCacheKey = key
651+ c.providerOptsCacheMu.Unlock()
652+
653+ return opts, temp, topP, topK, freqPenalty, presPenalty
654+}
655+
591656func (c *coordinator) buildAgent(ctx context.Context, prompt *prompt.Prompt, agent config.Agent, isSubAgent bool) (SessionAgent, error) {
592657 large, small, err := c.buildAgentModels(ctx, isSubAgent)
593658 if err != nil {
@@ -1221,15 +1286,34 @@ func (c *coordinator) UpdateModels(ctx context.Context) error {
12211286 }
12221287 c.currentAgent.SetModels(large, small)
12231288
1224− agentCfg, ok := c.cfg.Config().Agents[config.AgentCoder]
1289+ cfg := c.cfg.Config()
1290+ agentCfg, ok := cfg.Agents[config.AgentCoder]
12251291 if !ok {
12261292 return errCoderAgentNotConfigured
12271293 }
12281294
1295+ // Fast path: reuse cached tools when neither the config nor the MCP
1296+ // tool set has changed since the last build.
1297+ mcpGen := c.mcpToolsGen.Load()
1298+ c.toolsCacheMu.Lock()
1299+ if c.toolsCache != nil && c.toolsCacheCfg == cfg && c.toolsCacheMCP == mcpGen {
1300+ c.currentAgent.SetTools(c.toolsCache)
1301+ c.toolsCacheMu.Unlock()
1302+ return nil
1303+ }
1304+ c.toolsCacheMu.Unlock()
1305+
12291306 tools, err := c.buildTools(ctx, agentCfg, false)
12301307 if err != nil {
12311308 return err
12321309 }
1310+
1311+ c.toolsCacheMu.Lock()
1312+ c.toolsCache = tools
1313+ c.toolsCacheCfg = cfg
1314+ c.toolsCacheMCP = mcpGen
1315+ c.toolsCacheMu.Unlock()
1316+
12331317 c.currentAgent.SetTools(tools)
12341318 return nil
12351319}