Goro commited on
Commit
335b92a
·
1 Parent(s): 2dc14d6

Add file type example

Browse files
Files changed (1) hide show
  1. examples/get_file.py +27 -0
examples/get_file.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import aiohttp
2
+ from fastmcp.server import FastMCP
3
+ from fastmcp.utilities.types import File
4
+
5
+ def create_server():
6
+ mcp = FastMCP(name="File Demo", instructions="Get files from the server or URL.")
7
+
8
+ @mcp.tool()
9
+ async def get_test_file_from_server(path: str = "requirements.txt") -> File:
10
+ """
11
+ Get a test file from the server. If the path is not provided, it defaults to 'requirements.txt'.
12
+ """
13
+ return File(path=path)
14
+
15
+ @mcp.tool()
16
+ async def get_test_pdf_from_url(url: str = "https://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf") -> File:
17
+ """
18
+ Get a test PDF file from a URL. If the URL is not provided, it defaults to a sample PDF.
19
+ """
20
+ async with aiohttp.ClientSession() as session:
21
+ async with session.get(url) as response:
22
+ pdf_data = await response.read()
23
+ return File(data=pdf_data, format="pdf")
24
+
25
+ return mcp
26
+ if __name__ == "__main__":
27
+ create_server().run(transport="sse", host="0.0.0.0", port=8001, path="/sse")