terms.pyโข9.01 kB
"""Term management tools for POEditor MCP server."""
from typing import Any, Dict, List, Optional
from mcp.types import Tool, TextContent, INVALID_PARAMS
from mcp import McpError
from ..poeditor_client import POEditorClient, POEditorError
def get_term_tools() -> List[Tool]:
"""Get all term-related tools."""
return [
Tool(
name="list_terms",
description="List terms in a project",
inputSchema={
"type": "object",
"properties": {
"project_id": {
"type": "string",
"description": "Project ID"
},
"language_code": {
"type": "string",
"description": "Language code (optional)"
}
},
"required": ["project_id"]
}
),
Tool(
name="add_term",
description="Add a term to a project",
inputSchema={
"type": "object",
"properties": {
"project_id": {
"type": "string",
"description": "Project ID"
},
"term": {
"type": "string",
"description": "The term/key to add"
},
"context": {
"type": "string",
"description": "Optional context for the term"
},
"reference": {
"type": "string",
"description": "Optional reference for the term"
},
"plural": {
"type": "string",
"description": "Optional plural form"
}
},
"required": ["project_id", "term"]
}
),
Tool(
name="update_term",
description="Update a term",
inputSchema={
"type": "object",
"properties": {
"project_id": {
"type": "string",
"description": "Project ID"
},
"term_id": {
"type": "string",
"description": "Term ID"
},
"term": {
"type": "string",
"description": "New term value"
},
"context": {
"type": "string",
"description": "New context"
},
"reference": {
"type": "string",
"description": "New reference"
},
"plural": {
"type": "string",
"description": "New plural form"
}
},
"required": ["project_id", "term_id"]
}
),
Tool(
name="delete_term",
description="Delete a term",
inputSchema={
"type": "object",
"properties": {
"project_id": {
"type": "string",
"description": "Project ID"
},
"term_id": {
"type": "string",
"description": "Term ID"
}
},
"required": ["project_id", "term_id"]
}
)
]
async def handle_list_terms(
client: POEditorClient,
arguments: Dict[str, Any]
) -> List[TextContent]:
"""Handle list_terms tool call."""
project_id = arguments.get("project_id")
if not project_id:
raise McpError(INVALID_PARAMS, "project_id is required")
language_code = arguments.get("language_code")
try:
terms = await client.list_terms(project_id, language_code)
if not terms:
return [TextContent(
type="text",
text=f"No terms found in project {project_id}."
)]
output = f"**Terms in Project {project_id}:**\n\n"
for term in terms[:20]: # Limit to first 20 terms
term_text = term.get('term', 'Unknown term')
term_id = term.get('definition_id', 'Unknown ID')
context = term.get('context', '')
output += f"- **{term_text}** (ID: {term_id})\n"
if context:
output += f" Context: {context}\n"
if len(terms) > 20:
output += f"\n... and {len(terms) - 20} more terms"
return [TextContent(type="text", text=output)]
except POEditorError as e:
raise McpError(INVALID_PARAMS, f"POEditor API error: {e}")
except Exception as e:
raise McpError(INVALID_PARAMS, f"Unexpected error: {e}")
async def handle_add_term(
client: POEditorClient,
arguments: Dict[str, Any]
) -> List[TextContent]:
"""Handle add_term tool call."""
project_id = arguments.get("project_id")
term = arguments.get("term")
if not project_id:
raise McpError(INVALID_PARAMS, "project_id is required")
if not term:
raise McpError(INVALID_PARAMS, "term is required")
context = arguments.get("context")
reference = arguments.get("reference")
plural = arguments.get("plural")
try:
result = await client.add_term(project_id, term, context, reference, plural)
output = f"**Term Added Successfully!**\n\n"
output += f"- **Project ID:** {project_id}\n"
output += f"- **Term:** {term}\n"
if context:
output += f"- **Context:** {context}\n"
if reference:
output += f"- **Reference:** {reference}\n"
if plural:
output += f"- **Plural:** {plural}\n"
return [TextContent(type="text", text=output)]
except POEditorError as e:
raise McpError(INVALID_PARAMS, f"POEditor API error: {e}")
except Exception as e:
raise McpError(INVALID_PARAMS, f"Unexpected error: {e}")
async def handle_update_term(
client: POEditorClient,
arguments: Dict[str, Any]
) -> List[TextContent]:
"""Handle update_term tool call."""
project_id = arguments.get("project_id")
term_id = arguments.get("term_id")
if not project_id:
raise McpError(INVALID_PARAMS, "project_id is required")
if not term_id:
raise McpError(INVALID_PARAMS, "term_id is required")
term = arguments.get("term")
context = arguments.get("context")
reference = arguments.get("reference")
plural = arguments.get("plural")
try:
result = await client.update_term(project_id, term_id, term, context, reference, plural)
output = f"**Term Updated Successfully!**\n\n"
output += f"- **Project ID:** {project_id}\n"
output += f"- **Term ID:** {term_id}\n"
if term:
output += f"- **Term:** {term}\n"
if context:
output += f"- **Context:** {context}\n"
if reference:
output += f"- **Reference:** {reference}\n"
if plural:
output += f"- **Plural:** {plural}\n"
return [TextContent(type="text", text=output)]
except POEditorError as e:
raise McpError(INVALID_PARAMS, f"POEditor API error: {e}")
except Exception as e:
raise McpError(INVALID_PARAMS, f"Unexpected error: {e}")
async def handle_delete_term(
client: POEditorClient,
arguments: Dict[str, Any]
) -> List[TextContent]:
"""Handle delete_term tool call."""
project_id = arguments.get("project_id")
term_id = arguments.get("term_id")
if not project_id:
raise McpError(INVALID_PARAMS, "project_id is required")
if not term_id:
raise McpError(INVALID_PARAMS, "term_id is required")
try:
success = await client.delete_term(project_id, term_id)
if success:
output = f"**Term {term_id} deleted successfully from project {project_id}!**"
else:
output = f"**Failed to delete term {term_id} from project {project_id}**"
return [TextContent(type="text", text=output)]
except POEditorError as e:
raise McpError(INVALID_PARAMS, f"POEditor API error: {e}")
except Exception as e:
raise McpError(INVALID_PARAMS, f"Unexpected error: {e}")
# Tool handler mapping
TERM_HANDLERS = {
"list_terms": handle_list_terms,
"add_term": handle_add_term,
"update_term": handle_update_term,
"delete_term": handle_delete_term,
}