figma-mcp
# Figma MCP (Model Context Protocol)
A comprehensive MCP (Model Context Protocol) integration for Figma that enables AI coding tools like Cursor to access, analyze, and prototype websites from Figma designs.
## š Features
- **š§ Smart File Handling** - Automatic size detection and recommendations
- **šØ Website Prototyping** - Generate CSS, HTML, and assets from Figma designs
- **š Page Navigation** - Comprehensive page indexing and navigation
- **š¾ File Management** - Save large files to JSON and manage downloads
- **š Component Analysis** - Analyze components, variants, and instances
- **šÆ Design Tokens** - Extract colors, typography, spacing, and styles
## What is MCP?
Model Context Protocol (MCP) is a standard for connecting AI models to external data sources and tools. It allows AI assistants to interact with various services, including Figma, through a standardized interface.
## Prerequisites
- Node.js (v18 or higher)
- npm or yarn
- A Figma account with API access
- Figma Personal Access Token
## Quick Start
```bash
# Clone the repository
git clone <your-repo-url>
cd figma-mcp-experiment
# Install dependencies
npm install
# Run setup wizard (creates .env and mcp-config.json)
npm run setup
# Start the MCP server
npm start
# Test the setup
npm run test:all
```
## Setup Instructions
### 1. Install Dependencies
```bash
npm install
```
### 2. Quick Setup (Recommended)
Run the interactive setup wizard:
```bash
npm run setup
```
This will guide you through:
- Creating your `.env` file with Figma API credentials
- Setting up `mcp-config.json` for MCP client configuration
- Configuring environment variables
### 3. Manual Configuration
If you prefer manual setup:
1. **Get Figma Access Token:**
- Go to [Figma Account Settings](https://www.figma.com/settings)
- Navigate to "Personal access tokens"
- Create a new token with appropriate permissions
2. **Create Environment File:**
```bash
cp env.example .env
```
Edit `.env`:
```env
FIGMA_ACCESS_TOKEN=your_figma_personal_access_token_here
FIGMA_FILE_KEY=your_figma_file_key_here
```
3. **Create MCP Configuration:**
```bash
cp mcp-config.example.json mcp-config.json
```
Edit `mcp-config.json`:
```json
{
"mcpServers": {
"figma-mcp": {
"command": "node",
"args": ["src/server.js"],
"cwd": ".",
"env": {
"FIGMA_ACCESS_TOKEN": "your_figma_access_token_here",
"FIGMA_FILE_KEY": "your_figma_file_key_here"
}
}
}
}
```
## Usage
### Starting the MCP Server
```bash
npm start
```
### Available Commands
- `npm run setup` - Interactive setup wizard
- `npm start` - Start the MCP server
- `npm run dev` - Start in development mode with hot reload
- `npm test` - Run all tests
- `npm run test:unit` - Run unit tests only
- `npm run test:integration` - Run integration tests only
- `npm run clean` - Remove generated files
- `npm run cleanup` - Full cleanup script
- `npm run debug` - Run debug script
## Available MCP Tools
### š§ Smart Tools (Start Here)
- **get_smart_figma_overview** - **RECOMMENDED FIRST STEP** - Automatic file analysis and recommendations
### š Core Tools
- **get_figma_file** - Retrieve complete Figma file data (ā ļø LIMITED: Only works for files under 200KB)
- **get_figma_file_nodes** - Get specific nodes from a Figma file
- **get_figma_comments** - Retrieve comments from a Figma file
- **create_figma_comment** - Add comments to Figma files
### ā” Lightweight Tools (For Large Files)
- **get_figma_file_metadata** - Get lightweight metadata and structure
- **get_figma_file_info** - **CONSOLIDATED** - Get file information (structure, stats, or specific page)
### š¾ File Management Tools
- **save_figma_file_to_json** - Save complete Figma file data to a JSON file (handles large files)
- **save_figma_nodes_to_json** - Save specific nodes from a Figma file to a JSON file
- **list_saved_figma_files** - List all saved JSON files in the downloads directory
### šØ Website Prototyping Tools
- **extract_figma_design_tokens** - Extract design tokens (colors, typography, spacing, etc.)
- **get_figma_layout_data** - Get layout and positioning data for nodes
- **analyze_figma_components** - Analyze component instances and variants
- **generate_figma_css** - Generate CSS from Figma design tokens
- **generate_figma_html** - Generate HTML structure from Figma layout data
- **get_figma_images** - **CONSOLIDATED** - Get image URLs or download images locally
### š§ Page Navigation Tools
- **get_figma_pages** - **CONSOLIDATED** - Get page information (list, index, detailed, or specific page)
## Smart File Handling (Recommended Approach)
**Always start with `get_smart_figma_overview`** - this tool automatically analyzes your file and recommends the best approach based on size and complexity.
### File Size Guidelines:
- **< 100KB**: Use `get_figma_file` directly
- **100-200KB**: Use lightweight tools with warnings
- **> 200KB**: Use `save_figma_file_to_json` to download, then work locally
### Recommended Workflows:
#### For Large Files (>200KB):
1. Use `save_figma_file_to_json` to download complete file
2. Use `get_figma_file_metadata` for overview
3. Use `get_figma_page_list` for navigation
4. Use `get_figma_file_page` for specific pages
5. Use `get_figma_file_nodes` for specific elements
#### For Multi-Page Files (>10 pages):
1. Use `get_figma_page_list` for page overview
2. Use `generate_figma_page_index` for detailed navigation
3. Use `get_figma_file_page` for specific pages
#### For Component-Rich Files (>50 components):
1. Use `analyze_figma_components` for component analysis
2. Use `get_figma_file_metadata` for overview
3. Use `get_figma_file_structure` for structure
## Handling Large Files (Legacy)
### 1. Start with Smart Overview (RECOMMENDED)
```javascript
// Always start here - gets automatic recommendations
const overview = await mcpClient.getSmartFileOverview({
fileKey: 'your_file_key'
});
// Follow the recommended approach
console.log(overview.recommendation.approach);
console.log(overview.recommendation.alternatives);
```
### 2. Use Lightweight Tools First
```javascript
// Get file overview without full content
const metadata = await mcpClient.getFileMetadata({
fileKey: 'your_file_key'
});
// Get file structure to understand organization
const structure = await mcpClient.getFileStructure({
fileKey: 'your_file_key'
});
// Get statistics to understand file complexity
const stats = await mcpClient.getFileStats({
fileKey: 'your_file_key'
});
```
### 2. Save Large Files to JSON
```javascript
// Save complete file to JSON (handles any size)
const saveResult = await mcpClient.saveFileToJson({
fileKey: 'your_file_key'
});
// Save specific nodes to JSON
const nodesResult = await mcpClient.saveNodesToJson({
fileKey: 'your_file_key',
ids: ['node_id_1', 'node_id_2']
});
```
### 3. Work with Specific Pages or Nodes
```javascript
// Get specific page content
const page = await mcpClient.getFilePage({
fileKey: 'your_file_key',
pageId: 'page_id'
});
// Get specific nodes
const nodes = await mcpClient.getFileNodes({
fileKey: 'your_file_key',
ids: ['node_id_1', 'node_id_2']
});
```
### 4. Create Website Prototypes
```javascript
// Extract design tokens for styling
const tokens = await mcpClient.extractDesignTokens({
fileKey: 'your_file_key'
});
// Get layout data for positioning
const layout = await mcpClient.getLayoutData({
fileKey: 'your_file_key',
ids: ['node_id_1', 'node_id_2']
});
// Generate CSS from design tokens
const css = await mcpClient.generateCSS({
fileKey: 'your_file_key',
outputPath: './styles.css'
});
// Generate HTML structure
const html = await mcpClient.generateHTML({
fileKey: 'your_file_key',
ids: ['node_id_1', 'node_id_2'],
outputPath: './index.html'
});
// Export images for assets
const images = await mcpClient.exportImages({
fileKey: 'your_file_key',
ids: ['node_id_1', 'node_id_2'],
format: 'png',
scale: 2
});
```
### 5. Navigate File Pages
```javascript
// Get page list for navigation
const pageList = await mcpClient.getPageList({
fileKey: 'your_file_key'
});
// Generate comprehensive page index
const pageIndex = await mcpClient.generatePageIndex({
fileKey: 'your_file_key',
includeDetails: true
});
// Get detailed page information
const pageInfo = await mcpClient.getPageInfo({
fileKey: 'your_file_key',
pageId: 'page_id'
});
```
## Example Usage
### š§ Smart File Handling (Recommended)
```javascript
// Always start with smart overview
const overview = await mcpClient.getSmartFileOverview({
fileKey: 'your_file_key'
});
// Follow the recommended approach
console.log(overview.recommendation.approach);
console.log(overview.recommendation.alternatives);
```
### š Basic File Operations
```javascript
// Get file metadata (lightweight)
const metadata = await mcpClient.getFileMetadata({
fileKey: 'your_file_key'
});
// Get specific nodes
const nodes = await mcpClient.getFileNodes({
fileKey: 'your_file_key',
ids: ['node_id_1', 'node_id_2']
});
// Get file structure
const structure = await mcpClient.getFileStructure({
fileKey: 'your_file_key'
});
```
### šØ Website Prototyping
```javascript
// Extract design tokens
const tokens = await mcpClient.extractDesignTokens({
fileKey: 'your_file_key'
});
// Generate CSS
const css = await mcpClient.generateCSS({
fileKey: 'your_file_key',
outputPath: './styles.css'
});
// Generate HTML
const html = await mcpClient.generateHTML({
fileKey: 'your_file_key',
ids: ['node_id_1', 'node_id_2'],
outputPath: './index.html'
});
// Get image URLs
const imageUrls = await mcpClient.getFigmaImages({
fileKey: 'your_file_key',
ids: ['node_id_1', 'node_id_2'],
format: 'png',
scale: 2,
download: false
});
// Download images locally
const downloadedImages = await mcpClient.getFigmaImages({
fileKey: 'your_file_key',
ids: ['node_id_1', 'node_id_2'],
format: 'png',
scale: 2,
download: true,
outputDir: './images'
});
```
### š§ Page Navigation
```javascript
// Get simple page list (lightweight)
const pageList = await mcpClient.getFigmaPages({
fileKey: 'your_file_key',
detailLevel: 'list'
});
// Get detailed page index
const pageIndex = await mcpClient.getFigmaPages({
fileKey: 'your_file_key',
detailLevel: 'index',
includeDetails: true
});
// Get specific page info
const pageInfo = await mcpClient.getFigmaPages({
fileKey: 'your_file_key',
pageId: 'page_id'
});
```
### š¾ File Management
```javascript
// Save large file to JSON
const saveResult = await mcpClient.saveFileToJson({
fileKey: 'your_file_key',
outputPath: './my-figma-file.json',
prettyPrint: true
});
// List saved files
const savedFiles = await mcpClient.listSavedFiles();
```
## Project Structure
```
āāā README.md # Project documentation
āāā CONTRIBUTING.md # Contributing guidelines
āāā CHANGELOG.md # Version history
āāā package.json # Project configuration
āāā mcp-config.json # MCP server configuration
āāā .env.example # Environment variables template
āāā .gitignore # Git ignore rules
āāā src/ # Source code
ā āāā server.js # MCP server implementation
ā āāā figma-client.js # Figma API client
ā āāā mcp-handlers.js # MCP tool handlers
āāā tests/ # Test files
ā āāā figma-client.test.js
ā āāā test-smart-file-handling.js
ā āāā test-new-tools.js
ā āāā test-file-saving.js
ā āāā test-prototyping-tools.js
ā āāā test-page-index.js
ā āāā examples/
ā āāā basic-usage.js
āāā scripts/ # Utility scripts
ā āāā debug-mcp.js
ā āāā start-server.sh
āāā downloads/ # Downloaded files (gitignored)
āāā exports/ # Exported files (gitignored)
```
## Testing
Run the test scripts to verify functionality:
```bash
# Run all tests
npm run test:all
# Run unit tests only
npm run test:unit
# Run integration tests only
npm run test:integration
# Run individual test files
node tests/test-smart-file-handling.js
node tests/test-new-tools.js
node tests/test-file-saving.js
node tests/test-prototyping-tools.js
node tests/test-page-index.js
```
## Development
### Project Structure
```
āāā src/ # Source code
ā āāā server.js # MCP server implementation
ā āāā figma-client.js # Figma API client
ā āāā mcp-handlers.js # MCP tool handlers
āāā tests/ # Test files
ā āāā figma-client.test.js
ā āāā test-*.js # Integration tests
ā āāā examples/ # Example usage
āāā scripts/ # Utility scripts
ā āāā debug-mcp.js
ā āāā start-server.sh
ā āāā cleanup.sh
āāā downloads/ # Downloaded files (gitignored)
āāā exports/ # Exported files (gitignored)
```
### Adding New Tools
1. Add the method to `src/figma-client.js`
2. Add the handler to `src/mcp-handlers.js`
3. Register the tool in `src/server.js`
4. Add tests in `tests/`
5. Update documentation
See [CONTRIBUTING.md](CONTRIBUTING.md) for detailed guidelines.
## Troubleshooting
### Common Issues
1. **Authentication Error**:
- Ensure your Figma access token is valid and has the correct permissions
- Check that the token is properly set in `.env` and `mcp-config.json`
2. **File Not Found**:
- Verify the file key is correct and you have access to the file
- Check that the file key is properly formatted
3. **Rate Limiting**:
- Figma API has rate limits; implement appropriate delays between requests
- Consider caching responses for frequently accessed data
4. **Large File Context Issues**:
- Use `get_smart_figma_overview` first to get recommendations
- Use lightweight tools or save files to JSON instead of getting full file data
- Follow the size-aware recommendations provided by the smart overview
5. **Configuration Issues**:
- Ensure both `.env` and `mcp-config.json` are properly configured
- Run `npm run setup` to recreate configuration files
- Check that file paths in `mcp-config.json` are correct
### Debug Mode
Enable debug logging by setting the `DEBUG` environment variable:
```bash
DEBUG=* npm start
```
### Getting Help
- Check the [PROJECT_SUMMARY.md](PROJECT_SUMMARY.md) for quick reference
- Review [CONTRIBUTING.md](CONTRIBUTING.md) for development guidelines
- See [CHANGELOG.md](CHANGELOG.md) for version history
- Run tests with `npm run test:all` to verify functionality
## Contributing
We welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for detailed guidelines.
### Quick Contribution Steps
1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests for new functionality
5. Update documentation
6. Submit a pull request
### Development Setup
```bash
# Clone and setup
git clone <your-fork-url>
cd figma-mcp-experiment
npm install
npm run setup
# Run tests
npm run test:all
# Start development
npm run dev
```
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## Resources
- [Figma API Documentation](https://www.figma.com/developers/api)
- [MCP Specification](https://modelcontextprotocol.io/)
- [Node.js Figma API Client](https://github.com/figma/figma-api)
- [Model Context Protocol](https://modelcontextprotocol.io/)
## Acknowledgments
- Figma for providing the excellent API
- The MCP community for the protocol specification
- Contributors and users of this project
TDQS
Scored across 17 tools
There is a dense cluster of file-retrieval tools (get_figma_file, get_figma_file_metadata, get_figma_file_info, get_figma_file_nodes, get_figma_pages, get_smart_figma_overview) whose boundaries rely entirely on prose hints like 'lightweight' or 'configurable detail level'. The descriptions do warn about size limits and recommend an entry point, which helps, but an agent can easily misselect between metadata/info/overview/pages for the same file.
Nearly all tools follow a verb_figma_noun snake_case pattern (get_figma_comments, create_figma_comment, save_figma_file_to_json, generate_figma_css). A few deviations exist, notably get_smart_figma_overview inserting an adjective and inconsistent pluralization (get_figma_comments vs create_figma_comment).
17 tools is at the heavy end of the acceptable range, and several of them (file/info/metadata/overview/pages) appear to be variations on the same read operation rather than distinct capabilities. The set feels padded by redundant access routes rather than cleanly scoped.
The extraction/inspection side is well covered (files, nodes, pages, tokens, images, layout, CSS/HTML generation), but the write surface is thin: only comment creation with no update/delete, and no way to list or search available Figma files. Descriptions also reference a get_figma_file_structure tool that does not exist in the set, which leaves a documented dead end.