Skip to content

Commit c5cffbd

Browse files
authored
Merge pull request #33 from mhmzdev/deduplication-v1
Deduplication | Reducing tokens by 45% almost
2 parents 12b1a5b + 0e5864e commit c5cffbd

File tree

15 files changed

+1426
-37
lines changed

15 files changed

+1426
-37
lines changed

.changeset/giant-crabs-rescue.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"figma-flutter-mcp": minor
3+
---
4+
5+
Break-even point: ~22 component analyses

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ node_modules/
22
.env
33
package-lock.json
44
dist/
5-
output/
5+
output/
6+
reports/

CONTRIBUTING.md

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
# Contributing to Figma Flutter MCP
2+
3+
Thank you for your interest in contributing to Figma Flutter MCP! This guide will help you get started with development and testing.
4+
5+
## 🚀 Quick Start for Contributors
6+
7+
### Prerequisites
8+
- Node.js 18+
9+
- npm or yarn
10+
- Figma API Key (for testing)
11+
- Git
12+
13+
### Setup Development Environment
14+
15+
1. **Fork and Clone**
16+
```bash
17+
git clone https://github.com/your-username/figma-flutter-mcp.git
18+
cd figma-flutter-mcp
19+
npm install
20+
```
21+
22+
2. **Create .env file**
23+
```bash
24+
# Create .env file with your Figma API key
25+
echo "FIGMA_API_KEY=your-figma-api-key-here" > .env
26+
```
27+
28+
3. **Start Development Server**
29+
```bash
30+
npm run dev
31+
```
32+
33+
## 📋 Development Guidelines
34+
35+
### Code Style
36+
- Use TypeScript for all new code
37+
- Follow existing code patterns and conventions
38+
- Use meaningful variable and function names
39+
- Add docs in `docs/` if you think its neccessary
40+
41+
### Project Structure
42+
```
43+
src/
44+
├── cli.mts # CLI entry point
45+
├── server.mts # MCP server implementation
46+
├── config.mts # Configuration handling
47+
├── extractors/ # Figma data extractors
48+
│ ├── colors/
49+
│ ├── components/
50+
│ ├── screens/
51+
│ └── typography/
52+
├── tools/ # Flutter code generators
53+
├── services/ # External service integrations
54+
├── types/ # TypeScript type definitions
55+
└── utils/ # Utility functions
56+
```
57+
58+
### Making Changes
59+
60+
1. **Create a Branch**
61+
```bash
62+
git checkout -b feature/your-feature-name
63+
# or
64+
git checkout -b fix/issue-description
65+
```
66+
67+
2. **Make Your Changes**
68+
- Write clean, documented code
69+
- Add tests for new features
70+
- Update documentation as needed
71+
72+
3. **Test Your Changes**
73+
```bash
74+
# Build and check for errors
75+
npm run build
76+
77+
# Test locally
78+
npm run dev
79+
```
80+
81+
4. **Commit and Push**
82+
```bash
83+
git add .
84+
git commit -m "feat: add new feature description"
85+
git push origin feature/your-feature-name
86+
```
87+
88+
5. **Create Pull Request**
89+
- Use descriptive titles and descriptions
90+
- Reference any related issues
91+
- Include screenshots/examples if applicable
92+
93+
## 🧪 Local Testing & Development
94+
95+
The project supports HTTP server mode for easier development and testing. This allows you to test MCP tools without setting up a full MCP client.
96+
97+
### Setting Up Your Environment
98+
99+
If you haven't already set up your Figma API key:
100+
```bash
101+
# Create .env file
102+
echo "FIGMA_API_KEY=your-figma-api-key-here" > .env
103+
```
104+
105+
**Get your Figma API Key:**
106+
1. Go to [Figma Settings > Personal Access Tokens](https://www.figma.com/developers/api#access-tokens)
107+
2. Generate a new personal access token
108+
3. Copy the token and add it to your `.env` file
109+
110+
⚠️ **Important**: Never commit your `.env` file to version control. It's already included in `.gitignore`.
111+
112+
### Development Server Options
113+
114+
#### Using npm scripts (recommended)
115+
```bash
116+
# Start HTTP server on default port 3333
117+
npm run dev
118+
119+
# Start HTTP server on a specific port
120+
npm run dev:port 4000
121+
122+
# Start in stdio mode (for MCP clients)
123+
npm run dev:stdio
124+
```
125+
126+
#### Using direct commands
127+
```bash
128+
# Start HTTP server
129+
npx tsx src/cli.mts --http
130+
131+
# Start HTTP server on specific port
132+
npx tsx src/cli.mts --http --port 4000
133+
134+
# Start in stdio mode
135+
npx tsx src/cli.mts --stdio
136+
```
137+
138+
#### Using built version
139+
```bash
140+
# Build first
141+
npm run build
142+
143+
# Start HTTP server
144+
node dist/cli.mjs --http
145+
146+
# Start HTTP server on specific port
147+
node dist/cli.mjs --http --port 4000
148+
```
149+
150+
## Connecting to the Server
151+
152+
### MCP Client Configuration
153+
154+
To connect an MCP client to the local HTTP server, add this configuration to your MCP JSON config file:
155+
156+
```json
157+
{
158+
"mcpServers": {
159+
"local-figma-flutter-mcp": {
160+
"url": "http://localhost:3333/mcp"
161+
}
162+
}
163+
}
164+
```
165+
166+
## Available Endpoints
167+
168+
When the HTTP server is running, the following endpoints are available:
169+
170+
- **POST /mcp** - Main Streamable HTTP endpoint for MCP communication
171+
- **GET /mcp** - Session management for StreamableHTTP
172+
- **DELETE /mcp** - Session termination for StreamableHTTP
173+
- **GET /sse** - Server-Sent Events endpoint (alternative transport)
174+
- **POST /messages** - Message endpoint for SSE transport
175+
176+
## Environment Variables
177+
178+
You can configure the server using environment variables. The recommended approach is to use a `.env` file:
179+
180+
### Using .env file (Recommended)
181+
```env
182+
# Required: Your Figma API key
183+
FIGMA_API_KEY=your-figma-api-key-here
184+
185+
# Optional: Enable HTTP mode by default
186+
HTTP_MODE=true
187+
188+
# Optional: Set default HTTP port
189+
HTTP_PORT=3333
190+
```
191+
192+
## 📋 Pull Request Checklist
193+
194+
Before submitting a PR:
195+
- [ ] Code builds without errors (`npm run build`)
196+
- [ ] Tests pass (if applicable)
197+
- [ ] Documentation updated
198+
- [ ] PR description explains changes
199+
- [ ] Related issues referenced
200+
- [ ] Follows existing code style
201+
202+
Thank you for contributing to Figma Flutter MCP! 🚀

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,37 @@ Once you've the FIGMA API KEY, you can setup the MCP in cursor as follows:
117117

118118
> NOTE: If you've installed this MCP as `npm` package make sure to keep it updated to latest version. Sometimes, it caches the old version and keep showing you error like "Not being able to use tool call" or "Figma API key setup is not working" etc.
119119
120-
### 🧑🏼‍💻 Local Setup
121-
Please ensure that in local setup your version remains updated with your local server, sometimes `npm i` has installed the server globally for you and the keeps on overriding your local changes because of which you might not see any update.
120+
121+
### 🚀 Quick Start for Local Testing
122+
123+
For quick local testing, you can run the server via HTTP instead of stdio:
124+
125+
```bash
126+
# Clone and setup
127+
git clone <your-repo-url> figma-flutter-mcp
128+
cd figma-flutter-mcp
129+
npm install
130+
131+
# Create .env file with your Figma API key
132+
echo "FIGMA_API_KEY=your-figma-api-key-here" > .env
133+
134+
# Start HTTP server for local testing
135+
npm run dev
136+
```
137+
138+
Then add this to your MCP client configuration:
139+
140+
```json
141+
{
142+
"mcpServers": {
143+
"local-figma-flutter": {
144+
"url": "http://localhost:3333/mcp"
145+
}
146+
}
147+
}
148+
```
149+
150+
See [CONTRIBUTING.md](CONTRIBUTING.md) for detailed instructions.
122151

123152
#### 0. Prerequisites
124153
- Node.js 18+

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,25 @@
1515
"scripts": {
1616
"build": "tsc",
1717
"start": "node dist/cli.mjs",
18-
"dev": "tsx src/cli.mts",
18+
"dev": "tsx src/cli.mts --http",
19+
"dev:stdio": "tsx src/cli.mts --stdio",
20+
"dev:port": "tsx src/cli.mts --http --port",
1921
"changeset": "changeset add",
2022
"version": "changeset version && npm install --lockfile-only",
2123
"release": "changeset publish"
2224
},
2325
"dependencies": {
2426
"@modelcontextprotocol/sdk": "^1.0.0",
2527
"dotenv": "^17.2.1",
28+
"express": "^4.18.2",
2629
"node-fetch": "^3.3.2",
2730
"yargs": "^17.7.2",
2831
"zod": "^3.22.4"
2932
},
3033
"devDependencies": {
3134
"@changesets/changelog-github": "^0.5.1",
3235
"@changesets/cli": "^2.29.6",
36+
"@types/express": "^4.17.21",
3337
"@types/node": "^20.10.0",
3438
"@types/node-fetch": "^2.6.9",
3539
"@types/yargs": "^17.0.32",

src/cli.mts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
#!/usr/bin/env node
22
import {getServerConfig} from './config.mjs';
3-
import {startMcpServer} from './server.mjs';
3+
import {startMcpServer, startHttpServer} from './server.mjs';
44

55
async function startServer(): Promise<void> {
66
const config = getServerConfig();
77

88
if (config.isStdioMode) {
99
await startMcpServer(config.figmaApiKey);
10+
} else if (config.isHttpMode) {
11+
console.log('Starting Figma Flutter MCP Server in HTTP mode...');
12+
await startHttpServer(config.httpPort, config.figmaApiKey);
1013
} else {
1114
console.log('Starting Figma Flutter MCP Server...');
1215
console.log('Use --stdio flag for MCP client communication');
16+
console.log('Use --http flag for local testing via HTTP');
17+
console.log('Use --help for more options');
1318
}
1419
}
1520

src/config.mts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ export interface ServerConfig {
77
figmaApiKey: string;
88
outputFormat: "yaml" | "json";
99
isStdioMode: boolean;
10+
isHttpMode: boolean;
11+
httpPort: number;
1012
configSources: {
1113
figmaApiKey: "cli" | "env";
1214
envFile: "cli" | "default";
1315
stdio: "cli" | "env" | "default";
16+
http: "cli" | "env" | "default";
17+
port: "cli" | "env" | "default";
1418
};
1519
}
1620

@@ -23,6 +27,8 @@ interface CliArgs {
2327
"figma-api-key"?: string;
2428
env?: string;
2529
stdio?: boolean;
30+
http?: boolean;
31+
port?: number;
2632
}
2733

2834
export function getServerConfig(): ServerConfig {
@@ -42,6 +48,16 @@ export function getServerConfig(): ServerConfig {
4248
description: "Run in stdio mode for MCP client communication",
4349
default: false,
4450
},
51+
http: {
52+
type: "boolean",
53+
description: "Run in HTTP mode for local testing",
54+
default: false,
55+
},
56+
port: {
57+
type: "number",
58+
description: "Port number for HTTP server",
59+
default: 3333,
60+
},
4561
})
4662
.help()
4763
.version(process.env.npm_package_version || "0.0.1")
@@ -66,10 +82,14 @@ export function getServerConfig(): ServerConfig {
6682
figmaApiKey: "",
6783
outputFormat: "json",
6884
isStdioMode: false,
85+
isHttpMode: false,
86+
httpPort: 3333,
6987
configSources: {
7088
figmaApiKey: "env",
7189
envFile: envFileSource,
7290
stdio: "default",
91+
http: "default",
92+
port: "default",
7393
},
7494
};
7595

@@ -91,6 +111,24 @@ export function getServerConfig(): ServerConfig {
91111
config.configSources.stdio = "env";
92112
}
93113

114+
// Handle HTTP mode
115+
if (argv.http) {
116+
config.isHttpMode = true;
117+
config.configSources.http = "cli";
118+
} else if (process.env.HTTP_MODE === "true") {
119+
config.isHttpMode = true;
120+
config.configSources.http = "env";
121+
}
122+
123+
// Handle port configuration
124+
if (argv.port) {
125+
config.httpPort = argv.port;
126+
config.configSources.port = "cli";
127+
} else if (process.env.HTTP_PORT) {
128+
config.httpPort = parseInt(process.env.HTTP_PORT, 10);
129+
config.configSources.port = "env";
130+
}
131+
94132
// Validate configuration
95133
if (!config.figmaApiKey) {
96134
console.error("Error: FIGMA_API_KEY is required (via CLI argument or .env file)");
@@ -105,6 +143,10 @@ export function getServerConfig(): ServerConfig {
105143
`- FIGMA_API_KEY: ${maskApiKey(config.figmaApiKey)} (source: ${config.configSources.figmaApiKey})`
106144
);
107145
console.log(`- STDIO_MODE: ${config.isStdioMode} (source: ${config.configSources.stdio})`);
146+
console.log(`- HTTP_MODE: ${config.isHttpMode} (source: ${config.configSources.http})`);
147+
if (config.isHttpMode) {
148+
console.log(`- HTTP_PORT: ${config.httpPort} (source: ${config.configSources.port})`);
149+
}
108150
console.log(); // Empty line for better readability
109151
}
110152

0 commit comments

Comments
 (0)