Fix Cloudflare Worker context management for ATXP MCP integration#63
Merged
robdimarco-atxp merged 3 commits intomainfrom Sep 19, 2025
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Breaking Changes
This is a breaking change for the
requirePaymentAPI, but provides much more reliable behavior in production environments.