André Oliveira commited on
Commit
62897a2
·
1 Parent(s): dc0d368

added new mcp upload tool

Browse files
Files changed (1) hide show
  1. app.py +16 -9
app.py CHANGED
@@ -41,13 +41,22 @@ def upload_docs_tool(files, docs_path="data/docs"):
41
  try:
42
  for f in files:
43
  if isinstance(f, str) and f.startswith(("http://", "https://")):
44
- # Download URL to a temp file
45
- fname = os.path.basename(f)
46
- tmp = tempfile.NamedTemporaryFile(delete=False)
47
- r = requests.get(f, stream=True)
48
- shutil.copyfileobj(r.raw, tmp)
 
 
 
 
 
 
 
 
49
  tmp.close()
50
  temp_files.append(tmp.name)
 
51
  files_payload.append(("files", open(tmp.name, "rb")))
52
 
53
  elif isinstance(f, str):
@@ -139,13 +148,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
139
  upload_mcp_btn = gr.Button("Upload via MCP", variant="primary")
140
 
141
  # MCP callable function
142
- def mcp_upload_wrapper(files_json, docs_path):
143
  """
144
  Upload documents to the server's docs folder via MCP.
145
  Accepts:
146
- - local file paths (str)
147
  - URLs (str)
148
- - file-like objects
149
  """
150
  import ast
151
  try:
@@ -155,7 +162,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
155
  return upload_docs_tool(files, docs_path)
156
 
157
  upload_mcp_btn.click(
158
- mcp_upload_wrapper,
159
  inputs=[upload_mcp_input, upload_mcp_path],
160
  outputs=upload_mcp_out
161
  )
 
41
  try:
42
  for f in files:
43
  if isinstance(f, str) and f.startswith(("http://", "https://")):
44
+ # Download URL to a temp file (txt aware)
45
+ resp = requests.get(f, timeout=60)
46
+ resp.raise_for_status()
47
+
48
+ # create temp file with proper extension
49
+ ext = os.path.splitext(f)[1] or ".txt"
50
+ tmp = tempfile.NamedTemporaryFile(delete=False, suffix=ext)
51
+
52
+ if "text" in resp.headers.get("Content-Type", "").lower():
53
+ tmp.write(resp.text.encode("utf-8"))
54
+ else:
55
+ tmp.write(resp.content)
56
+
57
  tmp.close()
58
  temp_files.append(tmp.name)
59
+
60
  files_payload.append(("files", open(tmp.name, "rb")))
61
 
62
  elif isinstance(f, str):
 
148
  upload_mcp_btn = gr.Button("Upload via MCP", variant="primary")
149
 
150
  # MCP callable function
151
+ def upload_urls_tool(files_json, docs_path):
152
  """
153
  Upload documents to the server's docs folder via MCP.
154
  Accepts:
 
155
  - URLs (str)
 
156
  """
157
  import ast
158
  try:
 
162
  return upload_docs_tool(files, docs_path)
163
 
164
  upload_mcp_btn.click(
165
+ upload_urls_tool,
166
  inputs=[upload_mcp_input, upload_mcp_path],
167
  outputs=upload_mcp_out
168
  )