mirror of
https://github.com/MoonshotAI/kimi-code.git
synced 2026-08-23 23:56:41 +00:00
2.6 KiB
2.6 KiB
agent-core Agent Guide
Hard rules
- The
Agentclass inpackages/agent-core/src/agentmust be usable on its own. The constructor must not force the caller to create aSessioninstance, nor require anagentIdorsession. It may accept an optionalsessionIdas a request-config hint — for example mapped to the provider'sprompt_cache_key— but the instance must not holdsessionId, and must not depend on the Session lifecycle, metadata, or parent/child relationship logic.
MCP management plane
src/mcp/registry.ts(McpServerRegistry) is the single config view for MCP servers:global(layered mcp.json files) /plugin(manifests, read-only, final effective config viaPluginManager.mcpServerEntries) /caller(SDK session injection). All management lookups insrc/rpc/core-impl.tsgo through it; mutations only accept mutable (user-level) entries and push changes into live sessions.- Live-session sync is one path:
KimiCore.reconcileMcpServerInSessionrecomputes the registry runtime target (resolveRuntimeTarget: enabled plugin > project layer > user file; caller injection shadows everything and is never touched) per (session, name) and drives the session to it. Never add mutation-path-specific connect/remove logic — extend the reconciliation. - Wire-facing config DTOs are redacted: session
McpServerEntry/McpServerInfo.configand read-onlyMcpManagedServerInfoentries carry thesrc/mcp/config-view.tsprojection (envKeys/headerKeysinstead of literalenv/headersvalues, which may hold credentials). Mutable user-level management entries keep full values for edit UIs. Core-internal code compares full configs viaMcpConnectionManager.getRawEntry. - One process-wide
McpOAuthServicelives onKimiCoreand is shared with everySession; eachSessionsubscribes to its credential events (save/invalidate/refresh-failed) in its constructor and unsubscribes on close, so even sessions still initializing see every event. Never construct a per-scope OAuth service in new code. Token writes go through the process-localOAuthTokenTransaction(@moonshot-ai/kimi-code-oauth), which serializes refresh grants per credential identity and stampsobtained_aton every durable write. Interactive authorization flows are serialized per credential too: a secondbeginAuthorizationfor the same identity joins the in-flight flow instead of resetting the shared provider's PKCE/state. Proactive refresh timers, their in-flight refreshes, and interactive flows live and die withMcpOAuthService.shutdown(), whichKimiCore.shutdown()awaits.