Skip to content

Commit 7b030c5

Browse files
Merge pull request #1 from frankmeszaros/fm/v0
wip
2 parents 5e08254 + dafd609 commit 7b030c5

File tree

5 files changed

+44
-23
lines changed

5 files changed

+44
-23
lines changed

apps/cloudflare-one-casb/src/index.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import OAuthProvider from '@cloudflare/workers-oauth-provider'
2-
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
32
import { McpAgent } from 'agents/mcp'
43
import { env } from 'cloudflare:workers'
54

65
import {
76
createAuthHandlers,
87
handleTokenExchangeCallback,
98
} from '@repo/mcp-common/src/cloudflare-oauth-handler'
9+
import { CloudflareMCPServer } from '@repo/mcp-common/src/server'
1010
import { registerAccountTools } from '@repo/mcp-common/src/tools/account'
1111

1212
import { registerIntegrationsTools } from './tools/integrations'
@@ -22,19 +22,13 @@ export type Props = {
2222
}
2323

2424
export type State = { activeAccountId: string | null }
25-
export class MyMCP extends McpAgent<Env, State, Props> {
26-
// @ts-ignore
27-
server = new McpServer({
28-
name: 'Remote MCP Server with Workers Observability',
29-
version: '1.0.0',
25+
export class CASBMCP extends McpAgent<Env, State, Props> {
26+
server = new CloudflareMCPServer(undefined, env.MCP_METRICS, {
27+
name: env.MCP_SERVER_NAME,
28+
version: env.MCP_SERVER_VERSION,
3029
})
3130

32-
initialState: State = {
33-
activeAccountId: null,
34-
}
35-
3631
async init() {
37-
// @ts-ignore
3832
registerAccountTools(this)
3933
registerIntegrationsTools(this)
4034
}
@@ -50,8 +44,6 @@ export class MyMCP extends McpAgent<Env, State, Props> {
5044
}
5145

5246
setActiveAccountId(accountId: string) {
53-
console.log('Setting account ID: ', accountId)
54-
// TODO: Figure out why this fail sometimes, and why we need to wrap this in a try catch
5547
try {
5648
this.setState({
5749
...this.state,
@@ -72,7 +64,7 @@ const CloudflareOneCasbScopes = {
7264
export default new OAuthProvider({
7365
apiRoute: '/sse',
7466
// @ts-ignore
75-
apiHandler: MyMCP.mount('/sse'),
67+
apiHandler: CASBMCP.mount('/sse'),
7668
// @ts-ignore
7769
defaultHandler: createAuthHandlers({ scopes: CloudflareOneCasbScopes }),
7870
authorizeEndpoint: '/oauth/authorize',

apps/cloudflare-one-casb/src/tools/integrations.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
assetCategoryVendorParam,
1616
} from '@repo/mcp-common/src/schemas/cf1-integrations'
1717

18-
import type { MyMCP } from '../index'
18+
import type { CASBMCP } from '../index'
1919

2020
const PAGE_SIZE = 3
2121

@@ -40,7 +40,10 @@ interface ToolDefinition<T extends Record<string, any>> {
4040
}
4141

4242
// Helper function to handle common error cases and account ID checks
43-
const withAccountCheck = <T extends Record<string, any>>(agent: MyMCP, handler: ToolHandler<T>) => {
43+
const withAccountCheck = <T extends Record<string, any>>(
44+
agent: CASBMCP,
45+
handler: ToolHandler<T>
46+
) => {
4447
return async (params: T) => {
4548
const accountId = agent.getActiveAccountId()
4649
if (!accountId) {
@@ -295,7 +298,7 @@ const toolDefinitions: Array<ToolDefinition<any>> = [
295298
* Registers the logs analysis tool with the MCP server
296299
* @param agent The MCP server instance
297300
*/
298-
export function registerIntegrationsTools(agent: MyMCP) {
301+
export function registerIntegrationsTools(agent: CASBMCP) {
299302
toolDefinitions.forEach(({ name, description, params, handler }) => {
300303
agent.server.tool(name, description, params, withAccountCheck(agent, handler))
301304
})

apps/cloudflare-one-casb/worker-configuration.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ declare namespace Cloudflare {
55
OAUTH_KV: KVNamespace;
66
CLOUDFLARE_CLIENT_ID: string;
77
CLOUDFLARE_CLIENT_SECRET: string;
8-
MCP_OBJECT: DurableObjectNamespace<import("./src/index").MyMCP>;
8+
MCP_OBJECT: DurableObjectNamespace<import("./src/index").CASBMCP>;
9+
10+
MCP_SERVER_NAME: "PLACEHOLDER";
11+
MCP_SERVER_VERSION: "PLACEHOLDER";
12+
MCP_METRICS: AnalyticsEngineDataset;
913
}
1014
}
1115
interface Env extends Cloudflare.Env {}

apps/cloudflare-one-casb/wrangler.jsonc

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,24 @@
99
"compatibility_flags": ["nodejs_compat"],
1010
"migrations": [
1111
{
12-
"new_sqlite_classes": ["MyMCP"],
12+
"new_sqlite_classes": ["CASBMCP"],
1313
"tag": "v1"
1414
}
1515
],
1616
"observability": {
1717
"enabled": true
1818
},
19+
"vars": {
20+
"MCP_SERVER_NAME": "PLACEHOLDER",
21+
"MCP_SERVER_VERSION": "PLACEHOLDER"
22+
},
1923
"dev": {
2024
"port": 8976
2125
},
2226
"durable_objects": {
2327
"bindings": [
2428
{
25-
"class_name": "MyMCP",
29+
"class_name": "CASBMCP",
2630
"name": "MCP_OBJECT"
2731
}
2832
]
@@ -35,6 +39,12 @@
3539
],
3640
"workers_dev": false,
3741
"preview_urls": false,
42+
"analytics_engine_datasets": [
43+
{
44+
"binding": "MCP_METRICS",
45+
"dataset": "mcp-metrics-dev"
46+
}
47+
],
3848
"env": {
3949
"staging": {
4050
"name": "mcp-cloudflare-casb-staging",
@@ -43,7 +53,7 @@
4353
"durable_objects": {
4454
"bindings": [
4555
{
46-
"class_name": "MyMCP",
56+
"class_name": "CASBMCP",
4757
"name": "MCP_OBJECT"
4858
}
4959
]
@@ -53,6 +63,12 @@
5363
"binding": "OAUTH_KV",
5464
"id": "18e839155d00407095d793dcf7e78f25"
5565
}
66+
],
67+
"analytics_engine_datasets": [
68+
{
69+
"binding": "MCP_METRICS",
70+
"dataset": "mcp-metrics-staging"
71+
}
5672
]
5773
},
5874
"production": {
@@ -62,7 +78,7 @@
6278
"durable_objects": {
6379
"bindings": [
6480
{
65-
"class_name": "MyMCP",
81+
"class_name": "CASBMCP",
6682
"name": "MCP_OBJECT"
6783
}
6884
]
@@ -72,6 +88,12 @@
7288
"binding": "OAUTH_KV",
7389
"id": "f9782295993747df90c29c45ca89edb1"
7490
}
91+
],
92+
"analytics_engine_datasets": [
93+
{
94+
"binding": "MCP_METRICS",
95+
"dataset": "mcp-metrics-production"
96+
}
7597
]
7698
}
7799
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ export function createAuthHandlers({
237237
label: user.email,
238238
},
239239
scope: oauthReqInfo.scope,
240-
// This will be available on this.props inside MyMCP
240+
// This will be available on this.props inside CASBMCP
241241
props: {
242242
user,
243243
accounts,

0 commit comments

Comments
 (0)