Skip to content

Commit 72d1f7d

Browse files
committed
fix: register name on the stub in the transport
1 parent 0e9a151 commit 72d1f7d

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
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: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,19 @@ export interface MCPStub {
171171
handleMcpMessage(
172172
message: JSONRPCMessage | JSONRPCMessage[]
173173
): Promise<JSONRPCMessage | JSONRPCMessage[] | undefined>;
174+
setName(name: string): Promise<void>;
174175
}
175176

176177
export interface RPCClientTransportOptions {
177178
stub: MCPStub;
178179
functionName?: string;
180+
doName?: string;
179181
}
180182

181183
export class RPCClientTransport implements Transport {
182184
private _stub: MCPStub;
183185
private _functionName: string;
186+
private _doName?: string;
184187
private _started = false;
185188
private _protocolVersion?: string;
186189

@@ -192,6 +195,7 @@ export class RPCClientTransport implements Transport {
192195
constructor(options: RPCClientTransportOptions) {
193196
this._stub = options.stub;
194197
this._functionName = options.functionName ?? "handleMcpMessage";
198+
this._doName = options.doName;
195199
}
196200

197201
setProtocolVersion(version: string): void {
@@ -229,6 +233,11 @@ export class RPCClientTransport implements Transport {
229233
validateJSONRPCMessage(message);
230234
}
231235

236+
// Set the name if the stub is a DO
237+
if (this._doName && "setName" in this._stub) {
238+
await this._stub.setName(this._doName);
239+
}
240+
232241
try {
233242
const result =
234243
await this._stub[this._functionName as keyof MCPStub](message);
@@ -425,11 +434,6 @@ export class RPCServerTransport implements Transport {
425434
// Use queueMicrotask to allow additional send() calls to accumulate
426435
// Resolver is reused for concurrent sends within the same tick
427436
queueMicrotask(() => resolver());
428-
} else if (this._currentRequestId !== null) {
429-
// This shouldn't happen - send() called after promise already resolved
430-
console.warn(
431-
`send() called after response already resolved for request ${this._currentRequestId}`
432-
);
433437
}
434438
}
435439

0 commit comments

Comments
 (0)