-
Notifications
You must be signed in to change notification settings - Fork 622
refactor(mcp): client manager hooks into standard AgentLifecycle #1895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
mattzcarey
wants to merge
1
commit into
main
Choose a base branch
from
feat/standardise-agent-lifecycle
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "agents": minor | ||
| --- | ||
|
|
||
| Make `MCPClientManager` a self-contained Agent lifecycle component. Construct it with `new MCPClientManager(this, { name, version })`; the previous standalone storage-only constructor is removed. Existing `Agent.mcp`, `addMcpServer()`, and MCP protocol behavior are preserved. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { AgentLifecycleRunner, type AgentLifecycle } from "../lifecycle"; | ||
|
|
||
| describe("AgentLifecycleRunner", () => { | ||
| it("runs startup hooks sequentially in component order", async () => { | ||
| const calls: string[] = []; | ||
| const components: AgentLifecycle[] = [ | ||
| { | ||
| async onStart() { | ||
| calls.push("first:start"); | ||
| await Promise.resolve(); | ||
| calls.push("first:end"); | ||
| } | ||
| }, | ||
| { | ||
| onStart() { | ||
| calls.push("second"); | ||
| } | ||
| } | ||
| ]; | ||
|
|
||
| const runner = new AgentLifecycleRunner(() => components); | ||
| await runner.onStart({ props: undefined }); | ||
|
|
||
| expect(calls).toEqual(["first:start", "first:end", "second"]); | ||
| }); | ||
|
|
||
| it("returns the first component response and stops dispatching", async () => { | ||
| const calls: string[] = []; | ||
| const request = new Request("https://example.com/callback"); | ||
| const expected = new Response("handled"); | ||
| const components: AgentLifecycle[] = [ | ||
| { | ||
| onRequest() { | ||
| calls.push("miss"); | ||
| return undefined; | ||
| } | ||
| }, | ||
| { | ||
| onRequest(context) { | ||
| calls.push(context.request.url); | ||
| return expected; | ||
| } | ||
| }, | ||
| { | ||
| onRequest() { | ||
| calls.push("too-late"); | ||
| return new Response("wrong"); | ||
| } | ||
| } | ||
| ]; | ||
|
|
||
| const runner = new AgentLifecycleRunner(() => components); | ||
|
|
||
| expect(await runner.onRequest({ request })).toBe(expected); | ||
| expect(calls).toEqual(["miss", request.url]); | ||
| }); | ||
|
|
||
| it("merges turn tools left to right", async () => { | ||
| const firstWeather = { source: "first" }; | ||
| const secondWeather = { source: "second" }; | ||
| const search = { source: "search" }; | ||
| const components: AgentLifecycle[] = [ | ||
| { | ||
| onTurn() { | ||
| return { tools: { weather: firstWeather } }; | ||
| } | ||
| }, | ||
| { | ||
| onTurn(context) { | ||
| expect(context.readiness).toEqual({ timeout: 1000 }); | ||
| return { tools: { weather: secondWeather, search } }; | ||
| } | ||
| } | ||
| ]; | ||
|
|
||
| const runner = new AgentLifecycleRunner(() => components); | ||
| const contribution = await runner.onTurn({ | ||
| readiness: { timeout: 1000 } | ||
| }); | ||
|
|
||
| expect(contribution.tools).toEqual({ weather: secondWeather, search }); | ||
| }); | ||
|
|
||
| it("stops a lifecycle phase when a component fails", async () => { | ||
| const calls: string[] = []; | ||
| const expected = new Error("startup failed"); | ||
| const runner = new AgentLifecycleRunner(() => [ | ||
| { | ||
| onStart() { | ||
| calls.push("first"); | ||
| throw expected; | ||
| } | ||
| }, | ||
| { | ||
| onStart() { | ||
| calls.push("second"); | ||
| } | ||
| } | ||
| ]); | ||
|
|
||
| await expect(runner.onStart({ props: undefined })).rejects.toBe(expected); | ||
| expect(calls).toEqual(["first"]); | ||
| }); | ||
|
|
||
| it("resolves replaced components when the lifecycle phase begins", async () => { | ||
| const calls: string[] = []; | ||
| let current: AgentLifecycle = { | ||
| onStart() { | ||
| calls.push("default"); | ||
| } | ||
| }; | ||
| const runner = new AgentLifecycleRunner(() => [current]); | ||
|
|
||
| current = { | ||
| onStart() { | ||
| calls.push("replacement"); | ||
| } | ||
| }; | ||
| await runner.onStart({ props: undefined }); | ||
|
|
||
| expect(calls).toEqual(["replacement"]); | ||
| }); | ||
|
|
||
| it("destroys components in reverse order", async () => { | ||
| const calls: string[] = []; | ||
| const components: AgentLifecycle[] = [ | ||
| { | ||
| onDestroy() { | ||
| calls.push("first"); | ||
| } | ||
| }, | ||
| { | ||
| onDestroy() { | ||
| calls.push("second"); | ||
| } | ||
| } | ||
| ]; | ||
|
|
||
| const runner = new AgentLifecycleRunner(() => components); | ||
| await runner.onDestroy({}); | ||
|
|
||
| expect(calls).toEqual(["second", "first"]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import path from "node:path"; | ||
| import { defineConfig } from "vitest/config"; | ||
|
|
||
| export default defineConfig({ | ||
| test: { | ||
| name: "agent-lifecycle", | ||
| environment: "node", | ||
| include: [path.join(import.meta.dirname, "**/*.test.ts")] | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| export type AgentStartContext<Props = unknown> = { | ||
| props: Props | undefined; | ||
| }; | ||
|
|
||
| export type AgentRequestContext = { | ||
| request: Request; | ||
| }; | ||
|
|
||
| export type AgentTurnReadiness = { timeout?: number } | undefined; | ||
|
|
||
| export type AgentTurnContext = { | ||
| readiness?: AgentTurnReadiness; | ||
| /** Whether components should contribute tools for this turn. Default true. */ | ||
| includeTools?: boolean; | ||
| }; | ||
|
|
||
| export type AgentTurnContribution = { | ||
| tools?: Record<string, unknown>; | ||
| }; | ||
|
|
||
| export type AgentDestroyContext = Record<string, never>; | ||
|
|
||
| export interface AgentLifecycle<Props = unknown> { | ||
| onStart?(context: AgentStartContext<Props>): void | Promise<void>; | ||
| onRequest?( | ||
| context: AgentRequestContext | ||
| ): Response | undefined | Promise<Response | undefined>; | ||
| onTurn?( | ||
| context: AgentTurnContext | ||
| ): AgentTurnContribution | void | Promise<AgentTurnContribution | void>; | ||
| onDestroy?(context: AgentDestroyContext): void | Promise<void>; | ||
| } | ||
|
|
||
| /** | ||
| * Runs lifecycle hooks for the components installed on an Agent. | ||
| * | ||
| * Components are resolved when each lifecycle phase begins rather than when | ||
| * the runner is constructed. This lets subclass field initializers replace a | ||
| * default component after `super()` without leaving the default registered. | ||
| * | ||
| * @internal | ||
| */ | ||
| export class AgentLifecycleRunner<Props = unknown> { | ||
| constructor( | ||
| private readonly resolveComponents: () => | ||
| | Iterable<AgentLifecycle<Props>> | ||
| | ReadonlyArray<AgentLifecycle<Props>> | ||
| ) {} | ||
|
|
||
| async onStart(context: AgentStartContext<Props>): Promise<void> { | ||
| for (const component of this.resolveComponents()) { | ||
| await component.onStart?.(context); | ||
| } | ||
| } | ||
|
|
||
| async onRequest(context: AgentRequestContext): Promise<Response | undefined> { | ||
| for (const component of this.resolveComponents()) { | ||
| const response = await component.onRequest?.(context); | ||
| if (response !== undefined) return response; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| async onTurn(context: AgentTurnContext): Promise<AgentTurnContribution> { | ||
| const tools: Record<string, unknown> = {}; | ||
| for (const component of this.resolveComponents()) { | ||
| const contribution = await component.onTurn?.(context); | ||
| if (contribution?.tools) Object.assign(tools, contribution.tools); | ||
| } | ||
| return { tools }; | ||
| } | ||
|
|
||
| async onDestroy(context: AgentDestroyContext): Promise<void> { | ||
| const components = [...this.resolveComponents()]; | ||
| for (let index = components.length - 1; index >= 0; index--) { | ||
| await components[index].onDestroy?.(context); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to think about this for a little bit. Maybe it's the right move, but it makes the interface contract between MCPClientManager and host agent implicit and invites tighter coupling than I'd like.