Skip to content

Fix Cloudflare Worker context management for ATXP MCP integration#63

Merged
robdimarco-atxp merged 3 commits intomainfrom
robdimarco/fix-cloudflare-process-bug
Sep 19, 2025
Merged

Fix Cloudflare Worker context management for ATXP MCP integration#63
robdimarco-atxp merged 3 commits intomainfrom
robdimarco/fix-cloudflare-process-bug

Conversation

@robdimarco-atxp
Copy link
Copy Markdown
Contributor

@robdimarco-atxp robdimarco-atxp commented Sep 19, 2025

Summary

This PR fixes the critical "No ATXP config found - payments cannot be processed" error that was occurring when ATXP Cloudflare Workers were deployed to production. The issue was caused because of a misunderstanding about how Cloudflare MCP Agents work.

In Cloudflare, the MCP Agent is a separate worker. This means we must assume it operates separately from the process with the HTTP request. The approach of sharing a variable will not work.

So we have to assume independent processes. This means that we cannot share a config and that request specific information such as the resource and the authentication token need to be sent as properties to the agent.

Test Results

  • ✅ All Cloudflare package tests passing (23/23)
  • ✅ Payment flow working in both development and production

Breaking Changes

This is a breaking change for the requirePayment API, but provides much more reliable behavior in production environments.

robdimarco-atxp and others added 3 commits September 19, 2025 14:46
This commit resolves the "No ATXP config found - payments cannot be processed"
error that occurred when deploying ATXP Cloudflare Workers to production.

## Changes:

### Architecture Changes
- **Removed global context storage**: Eliminated the race condition-prone global
  context variables that were causing context loss between request setup and
  payment processing
- **MCP Props-based context passing**: Context now flows through the MCP Agent
  props system, which is more reliable and thread-safe
- **Simplified API**: The `requirePayment` function now takes ATXP configuration
  arguments directly instead of relying on global state

### API Changes
- `requirePayment(paymentConfig, configOptions, mcpProps)` - New signature that
  takes configuration and context explicitly
- `ATXPMCPAgentProps` type - New type for context passed through MCP props
- `ATXPCloudflareOptions` extends `ATXPArgs` - Unified configuration interface

### Implementation Details
- **Context passing**: ATXP context (config, resource, tokenCheck) is now passed
  through the MCP framework's props system instead of global variables
- **Method reconstruction**: Removed complex object serialization reconstruction
  since we now use `buildServerConfig` to create fresh config objects
- **No AsyncLocalStorage dependency**: Eliminated the need for AsyncLocalStorage
  polyfills by using MCP's built-in context passing

### Test Updates
- Updated all tests to match the new API signatures
- Removed tests for deprecated context management functions
- Added tests for the new props-based approach

### Bug Fix
The root cause was a race condition where:
1. Request A sets global context during middleware processing
2. Request B overwrites the global context before Request A's MCP tool executes
3. Request A's tool execution fails with "No ATXP config found"

The new architecture eliminates this by ensuring each request carries its own
context through the MCP framework, making the system stateless and thread-safe.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@robdimarco-atxp robdimarco-atxp merged commit b4417ce into main Sep 19, 2025
1 check passed
@robdimarco-atxp robdimarco-atxp deleted the robdimarco/fix-cloudflare-process-bug branch September 19, 2025 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant