Skip to content

Commit 38f48c0

Browse files
committed
got the report generation working after the coding logic was corrected
1 parent 292b132 commit 38f48c0

4 files changed

Lines changed: 71 additions & 57 deletions

File tree

mcp_excel_server/mcpclient.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,22 +123,24 @@ async def process_query(self, query: str) -> str:
123123
)
124124

125125
final_text.append(response.content[0].text)
126-
127126
return "\n".join(final_text)
128127

129128
async def chat_loop(self):
130129
"""Run an interactive chat loop"""
131130
print("\nMCP Client Started!")
132131
print("Type your queries or 'quit' to exit.")
133-
132+
reply_history = ""
134133
while True:
135134
try:
136135
query = input("\nInteract with Excel File here: ").strip()
137-
136+
query_with_history = (
137+
f"Previous conversation:\n{reply_history}\n Query: {query}"
138+
)
138139
if query.lower() == "quit":
139140
break
140141

141-
response = await self.process_query(query)
142+
response = await self.process_query(query_with_history)
143+
reply_history += f"Query: {query}\nResponse: {response}\n"
142144
print("\n" + response)
143145

144146
except Exception as e:

mcp_excel_server/report_1951.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Exercise Data Analysis Report
2+
3+
## Summary Statistics
4+
5+
### Numerical Variables
6+
7+
#### Calories Burn
8+
- Mean: 309.68
9+
- Minimum: 103.34
10+
- Maximum: 497.55
11+
- Standard Deviation: 111.65
12+
13+
#### Dream Weight
14+
- Mean: 77.02
15+
- Minimum: 52.73
16+
- Maximum: 95.20
17+
- Standard Deviation: 12.39
18+
19+
#### Actual Weight
20+
- Mean: 77.32
21+
- Minimum: 54.14
22+
- Maximum: 97.44
23+
- Standard Deviation: 13.76
24+
25+
#### Age
26+
- Mean: 38.65
27+
- Minimum: 18
28+
- Maximum: 60
29+
- Standard Deviation: 13.81
30+
31+
#### Heart Rate
32+
- Mean: 137.40
33+
- Minimum: 103
34+
- Maximum: 174
35+
- Standard Deviation: 24.19
36+
37+
#### BMI
38+
- Mean: 28.80
39+
- Minimum: 20.45
40+
- Maximum: 34.72
41+
- Standard Deviation: 5.02
42+
43+
## Categorical Variables Distribution
44+
45+
### Gender
46+
- Male: 14 (70%)
47+
- Female: 6 (30%)
48+
49+
### Weather Conditions
50+
- Cloudy: 7 (35%)
51+
- Rainy: 6 (30%)
52+
- Sunny: 7 (35%)
53+
54+
### Exercise Intensity
55+
- 1-3 intensity level: 5 participants
56+
- 4-6 intensity level: 6 participants
57+
- 7-10 intensity level: 9 participants

mcp_excel_server/server.py

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@
33
import os
44
from pathlib import Path
55
from html import escape
6-
from anthropic import Anthropic
76

87

9-
anthropic = Anthropic()
108
mcp = FastMCP("mcp-excel-server")
119

12-
global analysis_data
13-
1410

1511
@mcp.tool()
1612
def 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()
13798
def 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

mcp_excel_server/test.xlsx

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)