WebashalarForML commited on
Commit
24d2ccd
·
verified ·
1 Parent(s): 4fb9a3e

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +72 -24
server.py CHANGED
@@ -147,32 +147,80 @@ SOURCE:
147
  </source_material>
148
  """
149
 
150
- @mcp.tool
151
- async def analyze_file(ctx: Context, file: bytes) -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  """
153
- Example tool that:
154
- 1. Receives a file
155
- 2. If type is ambiguous, asks for clarification
156
- 3. Then returns a processed summary
 
157
  """
158
-
159
- # 1) Basic file info
160
- info = f"Received file of size {len(file)} bytes."
161
-
162
- # 2) Elicit choice if needed
163
- result = await ctx.elicit(
164
- message="What is the file type (text/image/pdf)?",
165
- response_type=["text", "image", "pdf"]
166
- )
167
-
168
- if result.action == "accept":
169
- file_type = result.data
170
- else:
171
- return "Elicitation declined or cancelled"
172
-
173
- # 3) Do some processing based on type
174
- return f"{info} User said type: {file_type}"
175
-
 
 
 
 
 
 
 
 
 
 
 
 
 
176
  if __name__ == "__main__":
177
  print("Starting FastMCP server on port 7860...")
178
 
 
147
  </source_material>
148
  """
149
 
150
+ mcp = FastMCP("file-analyzer")
151
+
152
+ # Helper functions for different file types
153
+ async def process_pdf(file_bytes: bytes) -> str:
154
+ """Process PDF file and extract text/info"""
155
+ pdf_file = io.BytesIO(file_bytes)
156
+ reader = PdfReader(pdf_file)
157
+
158
+ num_pages = len(reader.pages)
159
+ text = ""
160
+ for page in reader.pages:
161
+ text += page.extract_text()
162
+
163
+ return f"PDF Analysis:\n- Pages: {num_pages}\n- Text length: {len(text)} chars\n\nFirst 500 chars:\n{text[:500]}"
164
+
165
+ async def process_image(file_bytes: bytes) -> str:
166
+ """Process image file"""
167
+ img = Image.open(io.BytesIO(file_bytes))
168
+ return f"Image Analysis:\n- Format: {img.format}\n- Size: {img.size}\n- Mode: {img.mode}"
169
+
170
+ async def process_text(file_bytes: bytes) -> str:
171
+ """Process text file"""
172
+ try:
173
+ text = file_bytes.decode('utf-8')
174
+ lines = text.split('\n')
175
+ words = len(text.split())
176
+ return f"Text Analysis:\n- Lines: {len(lines)}\n- Words: {words}\n- Characters: {len(text)}\n\nFirst 500 chars:\n{text[:500]}"
177
+ except UnicodeDecodeError:
178
+ return "Error: File is not valid UTF-8 text"
179
+
180
+ @mcp.tool
181
+ async def analyze_file(
182
+ ctx: Context,
183
+ file: bytes,
184
+ file_type: Optional[Literal["text", "image", "pdf"]] = None
185
+ ) -> str:
186
  """
187
+ Analyzes a file.
188
+
189
+ Args:
190
+ file: The file content
191
+ file_type: Type of file (text/image/pdf). If not provided, will ask user.
192
  """
193
+ # Basic info
194
+ info = f"Received file of size {len(file)} bytes.\n\n"
195
+
196
+ # If file_type not provided, elicit it from user
197
+ if file_type is None:
198
+ result = await ctx.elicit(
199
+ message="What is the file type?",
200
+ response_type=["text", "image", "pdf"]
201
+ )
202
+
203
+ if result.action == "accept":
204
+ file_type = result.data
205
+ else:
206
+ return "Elicitation cancelled by user"
207
+
208
+ # Route to appropriate processor
209
+ try:
210
+ if file_type == "pdf":
211
+ result = await process_pdf(file)
212
+ elif file_type == "image":
213
+ result = await process_image(file)
214
+ elif file_type == "text":
215
+ result = await process_text(file)
216
+ else:
217
+ return info + f"Unknown file type: {file_type}"
218
+
219
+ return info + result
220
+
221
+ except Exception as e:
222
+ return info + f"Error processing {file_type}: {str(e)}"
223
+
224
  if __name__ == "__main__":
225
  print("Starting FastMCP server on port 7860...")
226