server.py•2.56 kB
"""
FastMCP quickstart example.
cd to the `examples/snippets/clients` directory and run:
uv run server fastmcp_quickstart stdio
"""
import json
import pypdf
import docx
from mcp.server.fastmcp import FastMCP
# Create an MCP server
mcp = FastMCP("TestCaseGenerator")
# In-memory storage
parsed_docs = {}
exported_scenarios = {}
@mcp.tool()
def parse_document(file_path: str) -> str:
"""Read a PDF or DOCX and return plain text."""
if file_path.endswith(".pdf"):
reader = pypdf.PdfReader(file_path)
text = " ".join([page.extract_text() or "" for page in reader.pages])
elif file_path.endswith(".docx"):
doc = docx.Document(file_path)
text = " ".join([p.text for p in doc.paragraphs])
else:
raise ValueError("Only PDF and DOCX supported")
parsed_docs[file_path] = text
return text
# Tool 2: Save scenarios into a JSON file
@mcp.tool()
def export_scenarios(scenarios: dict, file_name: str = "scenarios.json") -> str:
"""Save generated test scenarios into a JSON file and store in memory."""
with open(file_name, "w") as f:
json.dump(scenarios, f, indent=2)
exported_scenarios[file_name] = scenarios
return file_name
# Prompt Claude can use for Hugging Face model
@mcp.prompt("test_scenario_prompt")
def test_scenario_prompt() -> str:
"""
Provide a prompt for generating structured test scenarios from user stories.
This prompt is used to convert user stories with acceptance criteria into
structured test scenarios. The output is expected to be a JSON object with
user story details, acceptance criteria, and corresponding test scenarios.
"""
return """
Convert user stories with acceptance criteria into structured test scenarios.
Return JSON with:
{
"UserStoryID": "...",
"AcceptanceCriteria": [
{
"ACID": "...",
"Description": "...",
"TestScenarios": [
{"Scenario": "...", "Given": "...", "When": "...", "Then": "..."}
]
}
]
}
"""
@mcp.resource("resource://TestCaseGenerator/parsed_documents")
def get_parsed_documents() -> dict:
"""List all parsed documents and their text."""
return parsed_docs
@mcp.resource("resource://TestCaseGenerator/exported_scenarios")
def get_exported_scenarios() -> dict:
"""List all exported scenario files and their contents."""
return exported_scenarios
if __name__ == "__main__":
print("TestCaseGenerator MCP server starting...")
mcp.run(transport="stdio")