69 lines
1.4 KiB
Python
69 lines
1.4 KiB
Python
from starlette.applications import Starlette
|
|
from starlette.routing import Mount, Host
|
|
|
|
from mcp.server.fastmcp import Context, FastMCP
|
|
|
|
from mcp.server.session import ServerSession
|
|
from mcp.types import SamplingMessage, TextContent
|
|
|
|
import json
|
|
|
|
|
|
from uuid import uuid4
|
|
from typing import List
|
|
from pydantic import BaseModel
|
|
|
|
|
|
mcp = FastMCP("Blog post generator")
|
|
|
|
# app = FastAPI()
|
|
|
|
posts = []
|
|
|
|
class BlogPost(BaseModel):
|
|
id: int
|
|
title: str
|
|
content: str
|
|
abstract: str
|
|
|
|
posts: List[BlogPost] = []
|
|
|
|
@mcp.tool()
|
|
async def create_blog(title: str, content: str, ctx: Context[ServerSession, None]) -> str:
|
|
"""Create a blog post and generate a summary"""
|
|
|
|
post = BlogPost(
|
|
id=len(posts) + 1,
|
|
title=title,
|
|
content=content,
|
|
abstract=""
|
|
)
|
|
|
|
prompt = f"Create an abstract of the following blog post: title: {title} and draft: {content} "
|
|
|
|
result = await ctx.session.create_message(
|
|
messages=[
|
|
SamplingMessage(
|
|
role="user",
|
|
content=TextContent(type="text", text=prompt),
|
|
)
|
|
],
|
|
max_tokens=100,
|
|
)
|
|
|
|
post.abstract = result.content.text
|
|
|
|
posts.append(post)
|
|
|
|
# return the complete blog post
|
|
return json.dumps({
|
|
"id": post.title,
|
|
"abstract": post.abstract
|
|
})
|
|
|
|
if __name__ == "__main__":
|
|
print("Starting server...")
|
|
# mcp.run()
|
|
mcp.run(transport="streamable-http")
|
|
|
|
# run app with: python server.py |