33import os
44from pathlib import Path
55from html import escape
6- from anthropic import Anthropic
76
87
9- anthropic = Anthropic ()
108mcp = FastMCP ("mcp-excel-server" )
119
12- global analysis_data
13-
1410
1511@mcp .tool ()
1612def xlsx_to_html_table (
@@ -98,41 +94,6 @@ def get_col_name(file_path: str = "test.xlsx", sheet_name: str = "Sheet1") -> st
9894 return f"There was issue in reading the data: { e } "
9995
10096
101- def analyse_table (
102- file_path : str = "test.xlsx" , sheet_name : str = "Sheet1" , has_headers : bool = True
103- ) -> str :
104- """Use this tool when the user specifically asks for an analysis of the given file.
105- The entire excel sheet is read into a html table format, and then analysed by an expert analyst.
106- Expect a feedback from the analyst on the report completion."""
107-
108- try :
109- final_html = xlsx_to_html_table (file_path , sheet_name , has_headers )
110- # sending the html to AI model for analysis
111-
112- hal_analysis_system = """You are hal4025, an expert in working on excel sheets.
113- You are very good in analysing xlsx files and providing the report.
114- Do not apologize. Do not provide guidance or examples.
115- Do not share what you cannot do.
116- Please do not provide the python code for the user requests.
117- Users don't know to code.
118- Do not explain how analyis is being done."""
119-
120- messages = [{"role" : "user" , "content" : final_html }]
121- print (final_html )
122- response = anthropic .messages .create (
123- model = "claude-3-5-haiku-20241022" ,
124- max_tokens = 1000 ,
125- system = hal_analysis_system ,
126- messages = messages ,
127- )
128- analysis_result = f"The analysis result { response .content [0 ].text } "
129-
130- return f"The analysis is ready. { analysis_result } ."
131-
132- except Exception as e :
133- return f"Unable to complete the analysis due to : { e } "
134-
135-
13697@mcp .tool ()
13798def read_excel_file_cell (
13899 file_path : str = "test.xlsx" ,
@@ -220,7 +181,7 @@ def search_excel_sheet_for_value(
220181 value_to_find : str = "5" ,
221182 match_case : bool = True ,
222183):
223- """Use this tool when a value_to_find has to to be located
184+ """Use this tool when user asks to find to value_to_find data
224185 inside a excel file at the file_path"""
225186 wb = load_workbook (file_path )
226187 ws = wb [sheet_name ]
@@ -243,15 +204,14 @@ def search_excel_sheet_for_value(
243204
244205
245206@mcp .tool ()
246- def write_analysis_md (
247- file_path : str = "analysis_report.md" ,
207+ def write_report (
208+ text_data : str ,
209+ file_path : str ,
248210):
249- """Use this tool whenthe analysis report has to be written to the file.
250- The data is taken from the analysis_data global variable, no need to look for data from outside.
211+ """Use this tool when the user wants some data to be written to the file.
251212 New report file will be generated every time, and you will get confirmation"""
252213 import uuid
253214
254- global analysis_data
255215 try :
256216 uid = uuid .uuid4 ()
257217 ver = str (uid )[:4 ]
@@ -262,15 +222,10 @@ def write_analysis_md(
262222 file_ver = f"{ filename } _{ ver } .md"
263223 else :
264224 file_ver = f"{ file_path } _{ ver } .md"
265- if analysis_data != "" :
266- with open (file_ver , "w" ) as anw :
267- anw .write (analysis_data )
268225
269- # after writing analysis data, reset the global variable
270- analysis_data = ""
271- return f"The report has been written to { file_ver } "
272- else :
273- return "The analysis data is empty. Kindly get the analysis done."
226+ with open (file_ver , "w" ) as anw :
227+ anw .write (text_data )
228+ return f"The report has been written to { file_ver } "
274229 except Exception as e :
275230 return f"There was issue in writing report: { e } "
276231
0 commit comments