Skip to content

Conversation

@plvo
Copy link

@plvo plvo commented Jun 13, 2025

Summary

This PR refactors the MCP plugin actions to use a centralized context pattern and improves logging consistency across the codebase.

Key Changes

Context-based Action Handler Pattern

  • Introduced useActionHandler utility that returns a complete ActionHandlerContext
  • Eliminates code duplication between callToolAction.ts and readResourceAction.ts
  • Centralizes state composition and MCP service retrieval
  • Uses spread operator pattern for cleaner function calls

Before:

const composedState = await runtime.composeState(message, ["RECENT_MESSAGES", "MCP"]);
const mcpService = runtime.getService<McpService>(MCP_SERVICE_NAME);
const mcpProvider = mcpService.getProviderData();

After:

const context = await useActionHandler({ actionName: ACTION_NAME, runtime, message, state, options, callback });
const toolSelectionName = await createToolSelectionName({...context});

Standardized Logging with mcpLogger

  • Replaced direct logger usage with centralized mcpLogger
  • Added consistent [MCP] prefixes and contextual tags ([CALLING], [SELECTED], [HANDLE])
  • Improved observability and debugging across MCP operations

Code Organization

  • Cleaned up imports using TypeScript's type imports
  • Moved utility functions to appropriate modules
  • Simplified error handling by leveraging context object
  • Added action name constants for better maintainability

Improved Error Handling

  • Consolidated error handling pattern using context spread
  • Reduced parameter passing in error functions
  • More consistent error handling across actions

Impact

  • Maintainability: Reduced code duplication and improved organization
  • Debugging: Better logging with contextual information
  • Consistency: Unified patterns across action handlers
  • Type Safety: Better TypeScript usage with proper type imports

- Updated `tsconfig.json` to include path mappings for easier imports.
- Replaced `logger` with `mcpLogger` in various files to standardize logging for MCP-related actions.
- Refactored action handlers (`callToolAction`, `readResourceAction`) to utilize the new logging mechanism and improve error handling.
- Removed deprecated resource selection schemas from `types.ts`.
- Improved validation and selection processes for tools and resources, ensuring better error reporting and handling.
- Cleaned up unused imports and optimized code structure for clarity and maintainability.
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