FeaturesTemplatesShowcaseTownie
AI
BlogDocsPricing
Log inSign up
c15r

c15r

Chat

Public
Like
Chat
Home
Code
20
backend
1
frontend
6
shared
2
test
4
AFFORDANCE-COMPONENT-GUIDE.md
AFFORDANCE-FRAMEWORK.md
AFFORDANCE-IMPLEMENTATION-SUMMARY.md
AFFORDANCE-MANAGER-INITIALIZATION.md
COMMAND-PALETTE-REVIEW.md
DEPENDENCY-INJECTION-REVIEW.md
IMPLEMENTATION-SUMMARY-AFFORDANCES.md
IMPLEMENTATION-SUMMARY.md
NextSteps-Examples.md
NextSteps-README.md
README.md
ResourceViewer-README.md
STREAMING-IMPROVEMENTS.md
TESTING.md
package.json
H
test-runner.ts
Branches
1
Pull requests
Remixes
1
History
Environment variables
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data โ€“ all from the browser, and deployed in milliseconds.
Sign up now
Code
/
AFFORDANCE-MANAGER-INITIALIZATION.md
Code
/
AFFORDANCE-MANAGER-INITIALIZATION.md
Search
7/7/2025
Viewing readonly version of main branch: v1359
View latest version
AFFORDANCE-MANAGER-INITIALIZATION.md

Affordance Manager Initialization - Implementation Summary

Problem Solved

The affordance manager was experiencing race conditions where client tools were called before the manager was fully initialized, leading to "Affordance manager not available" errors. This has been fixed with a comprehensive solution that ensures:

  1. Singleton Pattern: Only one affordance manager instance is created per session
  2. Promise-based Initialization: Proper async initialization with waiting mechanisms
  3. Immediate Availability: Manager is created as soon as possible, not waiting for MCP connections
  4. Reliable Access: Client tools wait for manager availability with detailed error reporting

Key Changes Made

1. AffordanceManager - Singleton Pattern Implementation

Before:

  • Multiple instances could be created
  • No coordination between creation attempts
  • Direct constructor access

After:

  • Static getInstance() method ensures singleton
  • Promise-based initialization prevents race conditions
  • Private constructor prevents direct instantiation
  • isInitialized() method for status checking
  • reset() method for cleanup

2. App.tsx - Improved Initialization Logic

Before:

  • Required MCP clients to be connected before creating manager
  • Could create multiple instances
  • Restoration tied to creation

After:

  • Creates manager immediately when client pool is available
  • Separates manager creation from affordance restoration
  • Defers restoration until MCP clients connect (if needed)
  • Better logging and error handling

3. Client Tools - Promise-based Access

Before:

  • Immediate failure if manager not available
  • Simple error messages
  • No waiting mechanism

After:

  • getAffordanceManager() function waits up to 15 seconds
  • Detailed error messages with status information
  • Progressive logging during wait
  • Fallback to passed manager parameter

4. Enhanced Status Checking

New Tool: check_affordance_status

  • Reports detailed initialization status
  • Shows MCP client pool availability
  • Indicates singleton initialization state
  • Provides debugging information

Initialization Flow

1. App starts โ†’ Config loads โ†’ Client pool created
2. AffordanceManager.getInstance() called immediately
3. Singleton instance created and made globally available
4. If MCP clients connected OR no servers configured:
   โ†’ Restore persistent affordances immediately
5. If MCP servers configured but not connected:
   โ†’ Defer restoration until clients connect
6. Client tools use getAffordanceManager() which:
   โ†’ Returns immediately if available
   โ†’ Waits up to 15 seconds with progress logging
   โ†’ Provides detailed error if timeout

Benefits

For Users

  • Immediate Availability: Affordances work as soon as app loads
  • No Race Conditions: Tools always work when system is ready
  • Better Error Messages: Clear feedback about what's happening
  • Offline Support: Works without MCP servers

For Developers

  • Predictable Lifecycle: Clear initialization sequence
  • Singleton Safety: No duplicate instances
  • Comprehensive Logging: Easy debugging
  • Status Monitoring: Built-in status checking

For Assistants

  • Reliable Interface: Tools work consistently
  • Status Visibility: Can check initialization progress
  • Clear Error Handling: Helpful error messages
  • Immediate Feedback: No silent failures

Usage Guidelines

For Assistant

  • Use check_affordance_status to monitor initialization
  • If tools return "not available" errors, wait and retry
  • Errors now include detailed status information
  • Persistent affordances restore automatically when ready

For Developers

  • Use AffordanceManager.getInstance() instead of constructor
  • Manager is available immediately after client pool creation
  • Check AffordanceManager.isInitialized() for status
  • Use AffordanceManager.reset() for cleanup

Error Scenarios Handled

  1. App Still Loading: Tools wait with progress logging
  2. MCP Servers Configured but Not Connected: Manager works, restoration deferred
  3. No MCP Servers: Manager works immediately
  4. Initialization Failure: Detailed error messages with status
  5. Multiple Initialization Attempts: Singleton prevents duplicates

Testing Considerations

  • Test with no MCP servers (offline mode)
  • Test with MCP servers that fail to connect
  • Test rapid tool calls during initialization
  • Test page refresh with persistent affordances
  • Test error scenarios and timeout handling

Debugging

Console Logs

  • [App] prefix for app-level initialization
  • [AffordanceManager] prefix for manager operations
  • [getAffordanceManager] prefix for tool access
  • Progress logging every 2 seconds during waits

Status Tool

Use check_affordance_status to get:

  • Manager availability status
  • MCP client pool status
  • Connected client count
  • Singleton initialization state
  • Detailed debugging information

This implementation ensures the affordance system is robust, predictable, and provides clear feedback about its readiness state.

FeaturesVersion controlCode intelligenceCLI
Use cases
TeamsAI agentsSlackGTM
ExploreDocsShowcaseTemplatesNewestTrendingAPI examplesNPM packages
PricingNewsletterBlogAboutCareersBrandhi@val.townStatus
X (Twitter)
Discord community
GitHub discussions
YouTube channel
Bluesky
Terms of usePrivacy policyAbuse contact
ยฉ 2025 Val Town, Inc.