Skip to content

Commit 1c0052c

Browse files
committed
Implement MCP server
with FastMCP
1 parent cb08a1b commit 1c0052c

File tree

3 files changed

+476
-22
lines changed

3 files changed

+476
-22
lines changed

firefly/mcp/server.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Firefly MCP server using FastMCP
2+
3+
# Standard library imports
4+
import os
5+
from typing import Optional
6+
7+
# Third-party imports
8+
from fastmcp import FastMCP
9+
10+
# Local imports
11+
from firefly.client import FireflyClient
12+
13+
mcp = FastMCP("Adobe Firefly Image Generation MCP Server")
14+
15+
16+
@mcp.tool()
17+
def generate_image(
18+
prompt: str,
19+
client_id: Optional[str] = None,
20+
client_secret: Optional[str] = None,
21+
num_variations: int = 1,
22+
style: Optional[dict] = None,
23+
structure: Optional[dict] = None,
24+
prompt_biasing_locale_code: Optional[str] = None,
25+
negative_prompt: Optional[str] = None,
26+
seed: Optional[int] = None,
27+
aspect_ratio: Optional[str] = None,
28+
output_format: Optional[str] = None,
29+
content_class: Optional[str] = None,
30+
) -> dict:
31+
"""
32+
Generate an image using Adobe Firefly API.
33+
Args:
34+
prompt (str): The text prompt for image generation.
35+
client_id (str, optional): Firefly client ID. If not provided, uses FIREFLY_CLIENT_ID env var.
36+
client_secret (str, optional): Firefly client secret. If not provided, uses FIREFLY_CLIENT_SECRET env var.
37+
num_variations, style, structure, prompt_biasing_locale_code, negative_prompt, seed, aspect_ratio, output_format, content_class: FireflyClient.generate_image params.
38+
Returns:
39+
dict: The Firefly image response as a dict.
40+
"""
41+
42+
client_id = client_id or os.environ.get("FIREFLY_CLIENT_ID")
43+
client_secret = client_secret or os.environ.get("FIREFLY_CLIENT_SECRET")
44+
if not client_id or not client_secret:
45+
raise ValueError(
46+
"client_id and client_secret must be provided as arguments or environment variables."
47+
)
48+
style_arg = style or None
49+
structure_arg = structure or None
50+
client = FireflyClient(client_id=client_id, client_secret=client_secret)
51+
response = client.generate_image(
52+
prompt=prompt,
53+
num_variations=num_variations,
54+
style=style_arg,
55+
structure=structure_arg,
56+
prompt_biasing_locale_code=prompt_biasing_locale_code,
57+
negative_prompt=negative_prompt,
58+
seed=seed,
59+
aspect_ratio=aspect_ratio,
60+
output_format=output_format,
61+
content_class=content_class,
62+
)
63+
return response.json() or {
64+
"size": {"width": response.size.width, "height": response.size.height},
65+
"outputs": [
66+
{"seed": o.seed, "image": {"url": o.image.url}} for o in response.outputs
67+
],
68+
"contentClass": response.contentClass,
69+
}
70+
71+
72+
if __name__ == "__main__":
73+
mcp.run()

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ dev = [
1818
cli = [
1919
"typer>=0.12.3",
2020
]
21+
mcp-server = [
22+
"fastmcp>=0.7.0"
23+
]
2124

2225
[project.scripts]
2326
firefly = "firefly.cli:app"

0 commit comments

Comments
 (0)