Skip to content

Commit f7da517

Browse files
committed
fix: setting name after hibernation
1 parent 0e9a151 commit f7da517

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

packages/agents/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,19 +1732,19 @@ export class Agent<
17321732
): Promise<MCPTransportOptions> {
17331733
const { serverName, namespace, options } = config;
17341734

1735-
const doId = namespace.idFromName(`rpc:${serverName}`);
1735+
const doName = `rpc:${serverName}`;
1736+
const doId = namespace.idFromName(doName);
17361737
const stub = namespace.get(doId) as unknown as DurableObjectStub<T>;
17371738

1738-
await stub.setName(`rpc:${serverName}`);
1739-
17401739
if (options?.transport?.props) {
17411740
await stub.updateProps(options.transport.props);
17421741
}
17431742

17441743
return {
17451744
type: "rpc",
17461745
stub,
1747-
functionName: options?.transport?.functionName
1746+
functionName: options?.transport?.functionName,
1747+
doName
17481748
};
17491749
}
17501750

packages/agents/src/mcp/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,21 @@ export abstract class McpAgent<
388388
message: JSONRPCMessage
389389
): Promise<JSONRPCMessage | JSONRPCMessage[] | undefined> {
390390
if (!this._transport) {
391+
this.props = await this.ctx.storage.get("props");
392+
393+
// Re-run init() to register tools on the server
394+
await this.init();
391395
const server = await this.server;
392-
this._transport = new RPCServerTransport(this.getRpcTransportOptions());
396+
397+
this._transport = this.initTransport();
398+
399+
if (!this._transport) {
400+
throw new Error("Failed to initialize transport");
401+
}
393402
await server.connect(this._transport);
403+
404+
// Reinitialize the server with any stored initialize request
405+
await this.reinitializeServer();
394406
}
395407

396408
if (!(this._transport instanceof RPCServerTransport)) {

packages/agents/src/mcp/rpc.ts

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,20 +167,38 @@ function validateJSONRPCMessage(message: unknown): message is JSONRPCMessage {
167167
);
168168
}
169169

170+
/**
171+
* Type for RPC handler functions that can process MCP messages
172+
*/
173+
export type MCPMessageHandler = (
174+
message: JSONRPCMessage | JSONRPCMessage[]
175+
) => Promise<JSONRPCMessage | JSONRPCMessage[] | undefined>;
176+
177+
/**
178+
* Base interface for objects that can handle MCP messages via RPC
179+
*/
170180
export interface MCPStub {
171-
handleMcpMessage(
172-
message: JSONRPCMessage | JSONRPCMessage[]
173-
): Promise<JSONRPCMessage | JSONRPCMessage[] | undefined>;
181+
handleMcpMessage: MCPMessageHandler;
182+
setName?(name: string): Promise<void>;
174183
}
175184

176-
export interface RPCClientTransportOptions {
177-
stub: MCPStub;
178-
functionName?: string;
185+
export interface RPCClientTransportOptions<
186+
TStub extends MCPStub = MCPStub,
187+
TMethod extends keyof TStub = "handleMcpMessage"
188+
> {
189+
stub: TStub;
190+
functionName?: TMethod extends string ? TMethod : never;
191+
doName?: string;
179192
}
180193

181-
export class RPCClientTransport implements Transport {
182-
private _stub: MCPStub;
183-
private _functionName: string;
194+
export class RPCClientTransport<
195+
TStub extends MCPStub = MCPStub,
196+
TMethod extends keyof TStub = "handleMcpMessage"
197+
> implements Transport
198+
{
199+
private _stub: TStub;
200+
private _functionName: TMethod extends string ? TMethod : "handleMcpMessage";
201+
private _doName?: string;
184202
private _started = false;
185203
private _protocolVersion?: string;
186204

@@ -189,9 +207,13 @@ export class RPCClientTransport implements Transport {
189207
onerror?: (error: Error) => void;
190208
onmessage?: (message: JSONRPCMessage, extra?: MessageExtraInfo) => void;
191209

192-
constructor(options: RPCClientTransportOptions) {
210+
constructor(options: RPCClientTransportOptions<TStub, TMethod>) {
193211
this._stub = options.stub;
194-
this._functionName = options.functionName ?? "handleMcpMessage";
212+
this._functionName = (options.functionName ??
213+
"handleMcpMessage") as TMethod extends string
214+
? TMethod
215+
: "handleMcpMessage";
216+
this._doName = options.doName;
195217
}
196218

197219
setProtocolVersion(version: string): void {
@@ -229,9 +251,14 @@ export class RPCClientTransport implements Transport {
229251
validateJSONRPCMessage(message);
230252
}
231253

254+
// Set the name if the stub is a DO
255+
if (this._doName && this._stub.setName) {
256+
await this._stub.setName(this._doName);
257+
}
258+
232259
try {
233-
const result =
234-
await this._stub[this._functionName as keyof MCPStub](message);
260+
const handler = this._stub[this._functionName] as MCPMessageHandler;
261+
const result = await handler(message);
235262

236263
if (!result) {
237264
return;

0 commit comments

Comments
 (0)