Skip to content

Conversation

@JuanjoFuchs
Copy link

Fix for GitHub Issue #10: Missing "type": "text" field in MCP responses

Problem Description

All tools in the Databricks MCP server were returning output missing the required "type": "text" field in their responses. This caused Pydantic validation errors when the output was parsed against the TextContent model, which requires both "type": "text" and "text" fields.

Root Cause

The response objects returned from the tools included only a "text" field but lacked the mandatory "type": "text" field, which is required by the TextContent model in the MCP Python SDK.

Example of the problematic pattern:

# Before (BROKEN)
return [{"text": json.dumps(result)}]
return [{"text": json.dumps({"error": str(e)})}]

Example of validation error:

Error executing tool <tool_name>: 1 validation error for <tool_name>_toolOutput
result.0.type
Field required [type=missing, input_value={'text': '...'}, input_type=dict]

Solution Implemented

1. Added a helper function _format_response()

def _format_response(content: Union[Dict[str, Any], str]) -> List[TextContent]:
    """
    Format response content to conform with TextContent model requirements.
    
    The TextContent model requires both "type" and "text" fields.
    
    Args:
        content: The content to format (dict or string)
        
    Returns:
        List of TextContent objects with proper formatting
    """
    text_content = json.dumps(content) if isinstance(content, dict) else str(content)
    return [{"type": "text", "text": text_content}]

2. Updated all tool return statements

Replaced all 76 instances of problematic return statements:

# After (FIXED)
return _format_response(result)
return _format_response({"error": str(e)})

Files Modified

  • databricks_mcp/server/databricks_mcp_server.py
    • Added _format_response() helper function
    • Updated 76 return statements across all tool implementations

Tools Fixed

All 38 tools in the server were updated, including:

  • Cluster management (list_clusters, create_cluster, terminate_cluster, etc.)
  • Job management (list_jobs, create_job, run_job, etc.)
  • Notebook management (list_notebooks, run_notebook, etc.)
  • DBFS operations (list_files, dbfs_put, dbfs_delete, etc.)
  • SQL operations (execute_sql, create_table, etc.)
  • Unity Catalog operations (list_catalogs, create_catalog, list_schemas, etc.)
  • Library management (install_library, uninstall_library, etc.)
  • Repository management (list_repos, create_repo, pull_repo, etc.)

Verification

  • ✅ Syntax validation passed
  • ✅ Helper function tests passed
  • ✅ All return statements correctly format responses with both "type": "text" and "text" fields

Impact

This fix resolves the validation errors that were preventing all MCP tools from functioning correctly. Tools now return properly formatted responses that conform to the MCP TextContent model specification.

adding `"type": ""type": "text"` to all tool responses
Update virtual environment path checks and activation to use $PSScriptRoot for correct relative path resolution. This ensures the script works regardless of the current working directory.
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.

2 participants