-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_server.py
More file actions
102 lines (78 loc) · 3.13 KB
/
Copy pathmcp_server.py
File metadata and controls
102 lines (78 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from pydantic import Field
from mcp.server.fastmcp import FastMCP
from mcp.server.fastmcp.prompts import base
mcp = FastMCP("DocumentMCP", log_level="ERROR")
docs = {
"deposition.md": "This deposition covers the testimony of Angela Smith, P.E.",
"report.pdf": "The report details the state of a 20m condenser tower.",
"financials.docx": "These financials outline the project's budget and expenditures.",
"outlook.pdf": "This document presents the projected future performance of the system.",
"plan.md": "The plan outlines the steps for the project's implementation.",
"spec.txt": "These specifications define the technical requirements for the equipment.",
}
@mcp.tool (
name="read_doc_contents",
description="Read the contents of a document and return it as a string.",
)
def read_documents(
doc_id: str = Field(description="Id of the document to read"),
):
if doc_id not in docs:
raise ValueError(f"document with Id {doc_id} is not in docs.")
return docs[doc_id]
@mcp.tool (
name="edit_document",
description="Edit a document by replacing a string in the content by a new string.",
)
def edit_doc(
doc_id: str = Field(description="Id of the document to edit"),
old_string: str = Field(description="the text to replace must match exactly including white space"),
new_string: str = Field(description="the new text to input instead of the old text"),
):
if doc_id not in docs:
raise ValueError(f"document with Id {doc_id} is not in docs.")
docs[doc_id] = docs[doc_id].replace(old_string,new_string)
@mcp.resource ("docs://documents", mime_type="application/json")
def list_docs() -> list[str]:
return list(docs.keys())
@mcp.resource ("docs://documents/{doc_id}", mime_type="text/plain")
def fetch_doc(doc_id: str) -> str:
if doc_id not in docs:
raise ValueError(f"document with Id {doc_id} is not in docs.")
return docs[doc_id]
@mcp.prompt (
name = "format",
description = "rewrites the content of the document in the markdown format"
)
def format_document(doc_id: str = Field(description="Id of the document to read")) -> list[base.Message]:
prompt = f"""
Your goal is to reformat a document to be written with markdown syntax.
The id of the document you need to reformat is:
<document_id>
{doc_id}
</document_id>
Add in headers, bullet points, tables, etc as necessary. Feel free to add in structure.
Use the 'edit_document' tool to edit the document. After the document has been reformatted...
"""
return [
base.UserMessage(prompt)
]
@mcp.prompt (
name = "summarize",
description = "summarize the content of the document"
)
def summarize_document(doc_id: str = Field(description="Id of the document to read")) -> list[base.Message]:
prompt = f"""
Your goal is to summarize a document.
The id of the document you need to reformat is:
<document_id>
{doc_id}
</document_id>
Feel free to add in structure.
Use the 'edit_document' tool to edit the document. After the document has been reformatted...
"""
return [
base.UserMessage(prompt)
]
if __name__ == "__main__":
mcp.run(transport="stdio")