@@ -2,6 +2,7 @@ import app from "./app";
2
2
import { McpAgent } from "agents/mcp" ;
3
3
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js" ;
4
4
import { z } from "zod" ;
5
+ import { DurableObject } from "cloudflare:workers" ;
5
6
6
7
// Import DoiT tool handlers
7
8
import {
@@ -58,6 +59,24 @@ import { executeToolHandler } from "../../src/utils/toolsHandler.js";
58
59
import { zodSchemaToMcpTool } from "../../src/utils/util.js" ;
59
60
import { prompts } from "../../src/utils/prompts.js" ;
60
61
62
+ // Context Storage Durable Object
63
+ export class ContextStorage extends DurableObject {
64
+ constructor ( ctx : DurableObjectState , env : Env ) {
65
+ super ( ctx , env ) ;
66
+ }
67
+
68
+ async saveContext ( customerContext : string ) : Promise < void > {
69
+ console . log ( "Saving customer context:" , customerContext , this . ctx . id ) ;
70
+ await this . ctx . storage . put ( "customerContext" , customerContext ) ;
71
+ }
72
+
73
+ async loadContext ( ) : Promise < string | null > {
74
+ const context = await this . ctx . storage . get < string > ( "customerContext" ) ;
75
+ console . log ( "Loaded customer context:" , context , this . ctx . id ) ;
76
+ return context || null ;
77
+ }
78
+ }
79
+
61
80
// Helper function to convert DoiT handler response to MCP format
62
81
function convertToMcpResponse ( doitResponse : any ) {
63
82
if ( doitResponse . content && Array . isArray ( doitResponse . content ) ) {
@@ -80,35 +99,44 @@ export class DoitMCPAgent extends McpAgent {
80
99
return this . props . apiKey as string ;
81
100
}
82
101
83
- // Persist props to Durable Object storage
102
+ // Persist props to Context Storage Durable Object
84
103
private async saveProps ( ) : Promise < void > {
85
- if ( this . ctx ?. storage ) {
86
- await this . ctx . storage . put ( "persistedProps" , this . props . customerContext ) ;
104
+ const env = this . env as Env ;
105
+ const apiKey = this . props . apiKey as string ;
106
+ const customerContext = this . props . customerContext as string ;
107
+ if ( apiKey && env ?. CONTEXT_STORAGE ) {
108
+ const id = env . CONTEXT_STORAGE . idFromName ( apiKey ) ;
109
+ const contextStorage = env . CONTEXT_STORAGE . get ( id ) ;
110
+ await contextStorage . saveContext ( customerContext ) ;
87
111
}
88
112
}
89
113
90
- // Load props from Durable Object storage
91
- private async loadPersistedProps ( ) : Promise < void > {
92
- if ( this . ctx ?. storage ) {
93
- const persistedProps = await this . ctx . storage . get < string > (
94
- "persistedProps"
95
- ) ;
114
+ // Load props from Context Storage Durable Object
115
+ private async loadPersistedProps ( ) : Promise < string | null > {
116
+ const env = this . env as Env ;
117
+ const apiKey = this . props . apiKey as string ;
118
+ if ( apiKey && env ?. CONTEXT_STORAGE ) {
119
+ const id = env . CONTEXT_STORAGE . idFromName ( apiKey ) ;
120
+ const contextStorage = env . CONTEXT_STORAGE . get ( id ) ;
121
+ const persistedProps = await contextStorage . loadContext ( ) ;
96
122
if ( persistedProps ) {
97
- console . log ( "Loading persisted props:" , persistedProps ) ;
98
123
// Update props with persisted values
99
124
this . props . customerContext = persistedProps ;
125
+ return persistedProps ;
100
126
}
101
127
}
128
+ return ( this . props . customerContext as string ) || null ;
102
129
}
103
130
104
131
// Generic callback factory for tools
105
132
private createToolCallback ( toolName : string ) {
106
133
return async ( args : any ) => {
107
134
const token = this . getToken ( ) ;
135
+ const customerContext = await this . loadPersistedProps ( ) ;
108
136
109
137
const argsWithCustomerContext = {
110
138
...args ,
111
- customerContext : this . props . customerContext ,
139
+ customerContext,
112
140
} ;
113
141
return await executeToolHandler (
114
142
toolName ,
0 commit comments