Magic 21st_magic_component_builder returns [object Object]

View original issue on GitHub  ·  Variant 3

`magic-mcp`: `mcp__magic__21st_magic_component_builder` Returns `[object Object]`

The mcp__magic__21st_magic_component_builder tool within the magic-mcp project is exhibiting an issue where, instead of returning the expected React component code as a string, it returns [object Object]. This renders the tool's output unusable, as the actual component code is inaccessible to the client.

Root Cause Analysis

The underlying cause of this problem appears to stem from how the MCP (Magic Component Platform) server handles the response from the mcp__magic__21st_magic_component_builder tool. It seems the server is returning a structured JavaScript object representing the React component, but this object is not being properly serialized into a string before being transmitted back to the client. When JavaScript attempts to represent an unsorted object as a string, it defaults to [object Object]. This indicates a missing step in the data processing pipeline on the server-side.

Specifically, the server-side code likely needs to explicitly serialize the JavaScript object into a JSON string using JSON.stringify() before sending the response. Without this serialization, the client receives the raw object representation, leading to the observed [object Object] output.

Solution and Workaround

The ideal solution involves modifying the MCP server-side code to ensure that the output of mcp__magic__21st_magic_component_builder is correctly serialized to a JSON string before being sent to the client. The following code snippet illustrates the necessary change:


// Before (incorrect):
const componentObject = buildReactComponent(message, searchQuery, absolutePathToCurrentFile, absolutePathToProjectDirectory, standaloneRequestQuery);
return componentObject;

// After (correct):
const componentObject = buildReactComponent(message, searchQuery, absolutePathToCurrentFile, absolutePathToProjectDirectory, standaloneRequestQuery);
return JSON.stringify(componentObject);

This change ensures that the React component data is properly converted into a string format suitable for transmission and consumption by the client.

In the meantime, a workaround exists: using the mcp__magic__21st_magic_component_inspiration tool. This tool correctly returns a raw JSON array of components, providing a functional (though potentially less convenient) alternative. The data returned from this tool can then be parsed and used to construct the desired React components.

Practical Tips and Considerations

By addressing the root cause and implementing these best practices, the magic-mcp project can provide a more reliable and consistent experience for developers using the mcp__magic__21st_magic_component_builder tool.