er uses jiti to import tailwind.config.ts or tailwind.config.js at runtime. This eliminates the need for ts-node or pre-compilation steps, allowing the agent to read TypeScript configs directly.
2. Config Merging: It invokes tailwindcss/resolveConfig to merge your custom configuration with Tailwind's defaults. This produces the full, resolved theme object, including extended tokens and overridden values.
3. Token-Based Validation: Instead of running the full PostCSS/JIT pipeline (which is too slow for agent loops), the server performs O(1) token lookups. It validates classes by checking if they map to actual keys in the resolved theme.
4. Deep Syntax Parsing: The validator handles the complete Tailwind syntax tree. It strips modifiers (!, hover:, dark:), handles arbitrary values (bg-[#ff0000]), processes negative prefixes (-mt-4), and resolves multi-word variants (group-hover:).
Implementation Workflow
The agent integrates validation into its generation loop. Before emitting JSX, it queries the MCP server to verify class strings.
Example: Agent Orchestration Logic
The following TypeScript snippet demonstrates how an agent might use the tools to validate a component structure. Note the use of a custom prefix and extended tokens.
// Agent context: Project uses prefix 'tw-' and custom 'forest' colors
const agentWorkflow = async () => {
const mcpClient = getMcpClient();
const configPath = '/absolute/path/to/project/tailwind.config.ts';
// Step 1: Retrieve design system summary
const summary = await mcpClient.callTool('get_config_summary', {
config_path: configPath
});
// Result includes prefix: 'tw-', colors: { forest: { 300: '#...' } }
// Step 2: Validate a proposed class string
// Agent proposes: 'tw-bg-forest-300 p-6 rounded-lg flex grid'
const validation = await mcpClient.callTool('validate_class_string', {
class_string: 'tw-bg-forest-300 p-6 rounded-lg flex grid',
config_path: configPath
});
// Step 3: Analyze response and regenerate if needed
if (validation.invalid_classes.length > 0) {
console.error('Invalid classes detected:', validation.invalid_classes);
// Trigger regeneration with corrected classes
}
if (validation.warnings.length > 0) {
console.warn('Conflicts found:', validation.warnings);
// Example warning: "Conflicting multiple layout models: flex, grid"
}
// Step 4: Resolve specific tokens for dynamic generation
const tokens = await mcpClient.callTool('resolve_theme_tokens', {
config_path: configPath,
token_type: 'colors'
});
// Returns available color tokens to guide dynamic class construction
};
Available Tools:
get_config_summary: Returns high-level metadata (prefix, dark mode strategy, content globs).
resolve_theme_tokens: Queries specific token categories (colors, spacing, fonts) to inform dynamic class generation.
validate_class_string: The core validator. Returns valid_classes, invalid_classes, possibly_valid_classes, and warnings.
detect_css_conflicts: Specifically checks for mutually exclusive utilities (e.g., flex vs grid, block vs hidden).
Handling Plugins:
The validator cannot verify classes introduced by PostCSS plugins (e.g., @tailwindcss/forms or @tailwindcss/typography) without running the full CSS pipeline. When such classes are encountered, they are returned in possibly_valid_classes. The agent should treat these as valid if the corresponding plugins are known to be active in the project.
Pitfall Guide
Production adoption requires awareness of common integration errors and edge cases.
| Pitfall | Explanation | Fix |
|---|
| Tailwind v4 Incompatibility | Tailwind v4 uses a CSS-first configuration format. The programmatic resolveConfig API is deprecated/removed. The MCP server detects v4 and returns an error to prevent silent failures. | Ensure the project uses Tailwind v3. If migrating to v4, this tool is currently unsupported; wait for v4-specific tooling. |
Ignoring possibly_valid_classes | Agents may discard classes flagged as possibly_valid, breaking plugin functionality. The validator cannot confirm these without PostCSS. | Configure the agent to accept possibly_valid_classes as valid when plugin usage is detected or documented in the config summary. |
| Relative Config Paths | MCP clients often run in isolated contexts or different working directories. Passing a relative path (e.g., ./tailwind.config.ts) causes resolution failures. | Always pass absolute paths to the config_path argument. Use environment variables or IDE workspace roots to construct absolute paths dynamically. |
| Dynamic Class Construction | Template literals like `bg-${color}-500` cannot be validated statically. The validator requires concrete strings. | Resolve dynamic variables before validation. If the agent constructs classes dynamically, validate the resolved string, not the template. |
| Arbitrary Value Assumptions | The validator accepts arbitrary values (e.g., bg-[#fff]) as structurally valid but does not validate the content (e.g., hex correctness). | Trust the validator for syntax structure. Use separate linters or type checks if arbitrary value content validation is required. |
| Prefix Blindness | If the config defines a prefix (e.g., tw-), the agent must generate prefixed classes. Unprefixed classes will be flagged as invalid. | Ensure the agent reads the prefix from get_config_summary and applies it to all generated utilities. |
| Negative Spacing Misinterpretation | Classes like -mt-4 are valid if mt-4 exists. The validator strips the negative prefix for lookup. Agents may incorrectly assume negative classes are invalid if the base token is missing. | Rely on the validator's output. It correctly handles negative prefixes by checking the base token existence. |
Production Bundle
Action Checklist
Decision Matrix
| Scenario | Recommended Approach | Why | Cost Impact |
|---|
| Enterprise Project with Custom Design System | MCP Validation | Prevents token drift, ensures strict adherence to design tokens, reduces review overhead. | Medium (Setup time), High ROI (Review savings). |
| Small Project with Default Tailwind | Standard Agent | Overhead of MCP integration outweighs benefits for default configurations. | Low (No setup). |
| Tailwind v4 Migration | Manual/Alternative | Tool is incompatible with v4 CSS-based config. | High (Workaround required). |
| Heavy Plugin Usage | MCP with Plugin Awareness | Validator flags unknown classes; agent must be configured to handle possibly_valid output. | Medium (Agent logic complexity). |
Configuration Template
Add the following to your MCP client configuration (e.g., Claude Desktop, Cursor, or custom client). Replace the server name and args as needed.
{
"mcpServers": {
"tailwind-validator": {
"command": "npx",
"args": ["-y", "tailwind-context-resolver-mcp"]
}
}
}
Usage Note: When invoking tools, always include the config_path parameter:
{
"tool": "validate_class_string",
"arguments": {
"class_string": "tw-bg-accent p-4 hover:dark:bg-muted",
"config_path": "/Users/dev/project/src/tailwind.config.ts"
}
}
Quick Start Guide
- Add MCP Config: Insert the JSON template into your MCP client's configuration file.
- Restart Client: Reload your IDE or MCP client to register the new server.
- Test Validation: Ask the agent to validate a class string using
validate_class_string with your project's config path.
- Verify Output: Check that
valid_classes matches your expectations and invalid_classes catches errors.
- Deploy to Workflow: Update agent system prompts to require validation before UI code generation.