Cloudflare KV provides global, low-latency key-value storage for your MCP tools, enabling persistent data across sessions.
- Global Distribution: Data replicated across Cloudflare’s network
- Eventually Consistent: Fast reads with eventual consistency
- Generous Limits: 1GB storage and 100,000 operations daily on free tier
- Simple API: Easy key-value operations (get, put, delete, list)
Step 1: Create KV Namespace
What we're building A KV namespace that provides persistent, globally distributed storage for your MCP server data.
Why this matters KV storage enables stateful applications where data persists between AI conversations and user sessions.
# Create a new KV namespace for our MCP server
npx wrangler kv namespace create "TODO_STORE"
Setup Process:
- Run the command to create a new KV namespace
- Copy the namespace ID from the output
- Note both the binding name and ID for configuration
- Keep this terminal output for the next step
Namespace Created
✅ KV namespace "TODO_STORE" created
📋 ID: abc1234567890defghij
🔧 Ready for wrangler.jsonc binding
What we're building Worker configuration that binds the KV namespace to your MCP server, making it accessible via the TODO_STORE binding.
Why this matters Bindings connect external resources to your Worker, allowing secure access to KV storage from your code.
{
"kv_namespaces": [
{
"binding": "TODO_STORE",
"id": "abc1234567890defghij"
}
]
}
⚙️ Configuration Steps:
- Open your
wrangler.jsonc
file
- Add the KV namespace configuration
- Replace the ID with your actual namespace ID
- Save the file
- Run
npm run cf-typegen
to update types
Configuration Complete
✅ KV namespace bound to TODO_STORE
🔧 TypeScript types generated
💾 Ready for storage operations
What we're building A simple storage tool to test KV functionality and verify that data persists between requests.
Why this matters Testing storage operations ensures KV is properly configured before building complex stateful applications.
this.server.tool(
"storeValue",
"Store a simple key-value pair in Cloudflare KV",
{
key: z.string().describe("Key to store the value under"),
value: z.string().describe("Value to store")
},
async ({ key, value }) => {
try {
await this.env.TODO_STORE.put(key, value);
return {
content: [{
type: "text",
text: "Value stored successfully"
}]
};
} catch (error: any) {
throw new Error(`Failed to store value: ${error}`);
}
}
);
🧪 Testing Process:
- Add the storeValue tool to your MCP server
- Save and restart:
npm run dev
- Test with MCP Inspector
- Try storing:
{"key": "test", "value": "hello world"}
- Verify the tool works without errors
This simple tool demonstrates:
- put() Operation: Stores data in KV with a key
- Error Handling: Catches and reports storage failures
- Binding Access: Uses this.env.TODO_STORE from configuration
- Async Operations: Awaits KV operations for reliability
✅ KV storage tool working
💾 Data stored successfully
🔧 Ready to build stateful apps