"""
Performer tools for ThePornDB MCP Service.
Provides performer search and detailed profile retrieval functionality.
"""
import logging
from mcp.server.fastmcp import Context
from mcp.server.session import ServerSession
from mcp.types import CallToolResult, TextContent
from ..api import ThePornDBAPI
logger = logging.getLogger(__name__)
async def search_performers(
name: str,
ctx: Context[ServerSession, None] = None
):
"""
Search for performers by name.
Args:
name: Performer name to search
ctx: MCP context (injected automatically)
Returns:
SearchResponse with performer results
Raises:
CallToolResult: With error if validation fails
"""
# Input validation
if not name or not name.strip():
return CallToolResult(
content=[TextContent(
type="text",
text="Error: Performer name cannot be empty. Please provide a valid name."
)],
isError=True
)
# Get API client from context
if ctx and hasattr(ctx, 'request_context'):
api = ctx.request_context.lifespan_context.api
else:
return CallToolResult(
content=[TextContent(
type="text",
text="Error: Server context not available"
)],
isError=True
)
await ctx.info(f"Searching performers for '{name}'")
try:
# Call API
result = api.search_performers(name)
if result is None:
return CallToolResult(
content=[TextContent(
type="text",
text="Error: Performer search failed. Please try again."
)],
isError=True
)
await ctx.info(f"Found {len(result.get('data', []))} performers")
return result
except Exception as e:
logger.error(f"Error searching performers: {e}")
return CallToolResult(
content=[TextContent(
type="text",
text=f"Error: Failed to search performers - {str(e)}"
)],
isError=True
)
async def get_performer_details(
performer_id: str,
ctx: Context[ServerSession, None] = None
):
"""
Get complete performer profile with bio, measurements, aliases, and external links.
Args:
performer_id: Unique performer identifier
ctx: MCP context (injected automatically)
Returns:
Dictionary with complete performer details
Raises:
CallToolResult: With error if validation fails or performer not found
"""
# Input validation
if not performer_id or not performer_id.strip():
return CallToolResult(
content=[TextContent(
type="text",
text="Error: Performer ID cannot be empty. Please provide a valid performer ID."
)],
isError=True
)
# Get API client from context
if ctx and hasattr(ctx, 'request_context'):
api = ctx.request_context.lifespan_context.api
else:
return CallToolResult(
content=[TextContent(
type="text",
text="Error: Server context not available"
)],
isError=True
)
await ctx.info(f"Fetching details for performer/{performer_id}")
try:
# Call API
result = api.fetch_performer_details(performer_id)
if result is None:
return CallToolResult(
content=[TextContent(
type="text",
text=f"Error: Performer not found. The performer with ID '{performer_id}' does not exist."
)],
isError=True
)
await ctx.info(f"Successfully retrieved details for performer/{performer_id}")
return result
except Exception as e:
logger.error(f"Error fetching performer details: {e}")
return CallToolResult(
content=[TextContent(
type="text",
text=f"Error: Failed to fetch performer details - {str(e)}"
)],
isError=True
)