from mcp.types import SamplingMessage, TextContent
from mcp_use.server import Context, MCPServer
server = MCPServer(name="My Server")
@server.tool()
async def analyze_sentiment(text: str, ctx: Context) -> str:
"""Analyze the sentiment of text using the client's LLM."""
prompt = f"""Analyze the sentiment of the following text as positive, negative, or neutral.
Just output a single word - 'positive', 'negative', or 'neutral'.
Text to analyze: {text}"""
message = SamplingMessage(role="user", content=TextContent(type="text", text=prompt))
# Request LLM analysis
response = await ctx.sample(messages=[message])
if isinstance(response.content, TextContent):
return response.content.text.strip()
return ""