Skip to content

fix(mcp): use extra='ignore' in _json_schema_to_pydantic to allow CrewAI-injected security_context#4815

Closed
NIK-TIGER-BILL wants to merge 1 commit into
crewAIInc:mainfrom
NIK-TIGER-BILL:fix/mcp-security-context-extra-fields
Closed

fix(mcp): use extra='ignore' in _json_schema_to_pydantic to allow CrewAI-injected security_context#4815
NIK-TIGER-BILL wants to merge 1 commit into
crewAIInc:mainfrom
NIK-TIGER-BILL:fix/mcp-security-context-extra-fields

Conversation

@NIK-TIGER-BILL
Copy link
Copy Markdown
Contributor

@NIK-TIGER-BILL NIK-TIGER-BILL commented Mar 11, 2026

Problem

CrewAI's tool_usage.py injects a security_context field into every tool call's arguments before Pydantic validation (lines 1024-1045). MCP tools build their argument schema from the MCP server's inputSchema via create_model_from_schema(), which defaults to ConfigDict(extra='forbid').

Since the MCP server's schema never includes security_context, validation raises (closes #4796):

pydantic_core.ValidationError: Extra inputs are not permitted
  security_context
    Extra inputs are not permitted [type=extra_forbidden, ...]

Root cause

MCPToolResolver._json_schema_to_pydantic() calls create_model_from_schema() without overriding the default ConfigDict(extra='forbid').

Fix

Pass ConfigDict(extra='ignore') explicitly when building the schema model for MCP tools:

return create_model_from_schema(
    json_schema,
    model_name=model_name,
    enrich_descriptions=True,
    __config__=ConfigDict(extra="ignore"),   # ← allow security_context to pass through
)

Framework-injected fields are silently dropped before the model sees only the fields it declared. The MCP tool's own validation is unaffected.

Closes #4796


Note

Medium Risk
Relaxes Pydantic validation for MCP tool arguments by ignoring unknown fields, which could mask unexpected/typoed inputs but is scoped to MCP-derived schemas and prevents runtime failures from framework-injected metadata.

Overview
Fixes MCP tool argument validation failures by generating Pydantic models with extra="ignore" in MCPToolResolver._json_schema_to_pydantic, allowing CrewAI-injected fields like security_context to pass through without raising extra_forbidden errors.

This changes MCP tool schemas created via create_model_from_schema to silently drop unknown keys instead of rejecting them.

Written by Cursor Bugbot for commit 81037f4. This will update automatically on new commits. Configure here.

…wAI-injected security_context

CrewAI's tool_usage.py injects a 'security_context' field into every tool
call's arguments before Pydantic validation. MCP tools build their args
schema from the MCP server's inputSchema via create_model_from_schema(),
which defaults to ConfigDict(extra='forbid').

Since the MCP server's schema never includes 'security_context', validation
raises:
    pydantic_core.ValidationError: Extra inputs are not permitted (security_context)

Fix: pass ConfigDict(extra='ignore') explicitly when creating the schema
model for MCP tools, so framework-injected fields are silently dropped
before the model receives only the fields it declared.

Closes crewAIInc#4796
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.

[BUG] Pydantic Validation Error with security_context in MCP Tools

1 participant