Skip to content

Commit 6233099

Browse files
committed
Fix bug with props not being initialized in constructor
1 parent e840ccf commit 6233099

File tree

6 files changed

+73
-27
lines changed

6 files changed

+73
-27
lines changed

apps/sandbox-container/server/containerMcp.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,25 @@ import type { FileList } from '../shared/schema'
1313
import type { Props } from '.'
1414

1515
export class ContainerMcpAgent extends McpAgent<Env, {}, Props> {
16-
server: CloudflareMCPServer
16+
_server: CloudflareMCPServer | undefined
17+
set server(server: CloudflareMCPServer) {
18+
this._server = server
19+
}
20+
21+
get server(): CloudflareMCPServer {
22+
if (!this._server) {
23+
throw new Error('Tried to access server before it was initialized')
24+
}
25+
26+
return this._server
27+
}
1728

1829
constructor(
1930
public ctx: DurableObjectState,
2031
public env: Env
2132
) {
2233
console.log('creating container DO')
2334
super(ctx, env)
24-
this.server = new CloudflareMCPServer(
25-
this.props.user.id,
26-
this.env.MCP_METRICS,
27-
{
28-
name: this.env.MCP_SERVER_NAME,
29-
version: this.env.MCP_SERVER_VERSION,
30-
},
31-
{ instructions: BASE_INSTRUCTIONS }
32-
)
3335
}
3436

3537
async destroyContainer(): Promise<void> {
@@ -48,6 +50,17 @@ export class ContainerMcpAgent extends McpAgent<Env, {}, Props> {
4850
}
4951

5052
async init() {
53+
this.props.user.id
54+
this.server = new CloudflareMCPServer(
55+
this.props.user.id,
56+
this.env.MCP_METRICS,
57+
{
58+
name: this.env.MCP_SERVER_NAME,
59+
version: this.env.MCP_SERVER_VERSION,
60+
},
61+
{ instructions: BASE_INSTRUCTIONS }
62+
)
63+
5164
this.server.tool(
5265
'container_initialize',
5366
'Start or reset the container',

apps/sandbox-container/server/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ export default {
4141
fetch: (req: Request, env: Env, ctx: ExecutionContext) => {
4242
// @ts-ignore
4343
if (env.ENVIRONMENT === 'test') {
44-
ctx.props = {}
44+
ctx.props = {
45+
accessToken: 'foobar',
46+
user: {
47+
id: '123def',
48+
49+
},
50+
accounts: [],
51+
} as Props
4552
return ContainerMcpAgent.mount('/sse', { binding: 'CONTAINER_MCP_AGENT' }).fetch(
4653
req,
4754
env as Record<string, DurableObjectNamespace<McpAgent> | any>,

apps/workers-bindings/src/index.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,33 @@ export type Props = {
3232
}
3333

3434
export class WorkersBindingsMCP extends McpAgent<Env, WorkersBindingsMCPState, Props> {
35-
server: CloudflareMCPServer
35+
_server: CloudflareMCPServer | undefined
36+
set server(server: CloudflareMCPServer) {
37+
this._server = server
38+
}
39+
40+
get server(): CloudflareMCPServer {
41+
if (!this._server) {
42+
throw new Error('Tried to access server before it was initialized')
43+
}
44+
45+
return this._server
46+
}
3647

3748
initialState: WorkersBindingsMCPState = {
3849
activeAccountId: null,
3950
}
4051

4152
constructor(ctx: DurableObjectState, env: Env) {
4253
super(ctx, env)
54+
}
55+
56+
async init() {
4357
this.server = new CloudflareMCPServer(this.props.user.id, this.env.MCP_METRICS, {
4458
name: this.env.MCP_SERVER_NAME,
4559
version: this.env.MCP_SERVER_VERSION,
4660
})
47-
}
4861

49-
async init() {
5062
registerAccountTools(this)
5163
registerKVTools(this)
5264
registerWorkersTools(this)

apps/workers-observability/src/index.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,33 @@ export type Props = {
3131
export type State = { activeAccountId: string | null }
3232

3333
export class ObservabilityMCP extends McpAgent<Env, State, Props> {
34-
server: CloudflareMCPServer
34+
_server: CloudflareMCPServer | undefined
35+
set server(server: CloudflareMCPServer) {
36+
this._server = server
37+
}
38+
39+
get server(): CloudflareMCPServer {
40+
if (!this._server) {
41+
throw new Error('Tried to access server before it was initialized')
42+
}
43+
44+
return this._server
45+
}
3546

3647
initialState: State = {
3748
activeAccountId: null,
3849
}
3950

4051
constructor(ctx: DurableObjectState, env: Env) {
4152
super(ctx, env)
53+
}
54+
55+
async init() {
4256
this.server = new CloudflareMCPServer(this.props.user.id, this.env.MCP_METRICS, {
4357
name: this.env.MCP_SERVER_NAME,
4458
version: this.env.MCP_SERVER_VERSION,
4559
})
46-
}
4760

48-
async init() {
4961
registerAccountTools(this)
5062

5163
// Register Cloudflare Workers tools

packages/mcp-common/src/cloudflare-oauth-handler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { zValidator } from '@hono/zod-validator'
22
import { Hono } from 'hono'
33
import { z } from 'zod'
44

5-
import { AuthUser, type MetricsTracker } from '@repo/mcp-observability'
5+
import { AuthUser } from '@repo/mcp-observability'
66

77
import { getAuthorizationURL, getAuthToken, refreshAuthToken } from './cloudflare-auth'
88
import { McpError } from './mcp-error'
@@ -13,6 +13,7 @@ import type {
1313
TokenExchangeCallbackResult,
1414
} from '@cloudflare/workers-oauth-provider'
1515
import type { Context } from 'hono'
16+
import type { MetricsTracker } from '@repo/mcp-observability'
1617

1718
type AuthContext = {
1819
Bindings: {

packages/mcp-common/src/server.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { isPromise } from 'node:util/types'
22
import { type ServerOptions } from '@modelcontextprotocol/sdk/server/index.js'
3-
import { McpServer, type ToolCallback } from '@modelcontextprotocol/sdk/server/mcp.js'
4-
3+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
54
import { type ZodRawShape } from 'zod'
65

76
import { MetricsTracker, SessionStart, ToolCall } from '@repo/mcp-observability'
87

98
import { McpError } from './mcp-error'
109

10+
import type { ToolCallback } from '@modelcontextprotocol/sdk/server/mcp.js'
11+
1112
export class CloudflareMCPServer extends McpServer {
1213
private metrics
1314

1415
constructor(
15-
private userId: string | undefined,
16+
userId: string | undefined,
1617
wae: AnalyticsEngineDataset,
1718
serverInfo: {
1819
[x: string]: unknown
@@ -49,30 +50,30 @@ export class CloudflareMCPServer extends McpServer {
4950
// promise succeeds
5051
this.metrics.logEvent(
5152
new ToolCall({
52-
userId,
5353
toolName: name,
54+
userId,
5455
})
5556
)
5657
return r
5758
})
5859
.catch((e: any) => {
5960
// promise throws
60-
this.trackToolCallError(e, name)
61+
this.trackToolCallError(e, name, userId)
6162
throw e
6263
})
6364
} else {
6465
// non-promise succeeds
6566
this.metrics.logEvent(
6667
new ToolCall({
67-
userId,
6868
toolName: name,
69+
userId,
6970
})
7071
)
7172
return toolCall
7273
}
7374
} catch (e: any) {
7475
// non-promise throws
75-
this.trackToolCallError(e, name)
76+
this.trackToolCallError(e, name, userId)
7677
throw e
7778
}
7879
}
@@ -83,15 +84,15 @@ export class CloudflareMCPServer extends McpServer {
8384
}
8485
}
8586

86-
private trackToolCallError(e: any, toolName: string) {
87+
private trackToolCallError(e: any, toolName: string, userId?: string) {
8788
let errorCode = -1
8889
if (e instanceof McpError) {
8990
errorCode = e.code
9091
}
9192
this.metrics.logEvent(
9293
new ToolCall({
9394
toolName,
94-
userId: this.userId,
95+
userId: userId,
9596
errorCode: errorCode,
9697
})
9798
)

0 commit comments

Comments
 (0)