geronimo-pericoli commited on
Commit
b28944c
verified
1 Parent(s): d3cfc24

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +49 -25
server.py CHANGED
@@ -1,5 +1,7 @@
1
  from mcp.server.fastmcp import FastMCP
2
  from datetime import datetime
 
 
3
  import json
4
  import os
5
  import aiohttp # Necesario para las peticiones HTTP as铆ncronas
@@ -12,35 +14,57 @@ mcp = FastMCP("OnBase", port=port)
12
 
13
 
14
 
15
- @mcp.tool()
16
- def devolver_clave_secreta(
17
- clave_ingresada: str, # Input del usuario
18
- clave_secreta: str = "321" # Clave secreta definida
19
- ) -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  """
21
- Verifica si la clave ingresada coincide con la clave secreta.
22
- Si coincide, devuelve un JSON con la hora actual y un c贸digo.
23
- Si no coincide, devuelve un mensaje de error.
24
 
25
- Args:
26
- clave_ingresada: La clave que el usuario ingresa
27
- clave_secreta: La clave secreta para comparar (default: "321")
28
 
29
- Returns:
30
- str: JSON con hora y c贸digo o mensaje de error
31
- """
32
- if clave_ingresada == clave_secreta:
33
- respuesta = {
34
- "hora_actual": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
35
- "codigo": "codigo 123",
36
- "status": "success"
37
  }
38
- return json.dumps(respuesta, ensure_ascii=False)
39
- else:
40
- return json.dumps({
41
- "error": "Clave incorrecta",
42
- "status": "fail"
43
- }, ensure_ascii=False)
 
44
 
45
 
46
  @mcp.tool()
 
1
  from mcp.server.fastmcp import FastMCP
2
  from datetime import datetime
3
+ from llama_index import VectorStoreIndex, SimpleDirectoryReader
4
+ from typing import Dict, Optional
5
  import json
6
  import os
7
  import aiohttp # Necesario para las peticiones HTTP as铆ncronas
 
14
 
15
 
16
 
17
+
18
+ # Rutas a los documentos por fuente
19
+ DOCUMENTS_BASE_PATH = "./"
20
+ SOURCES = {
21
+ "oms": "oms/",
22
+ #"other": "other/",
23
+ #"other2": "other2/"
24
+ }
25
+
26
+ # Cargar 铆ndices (se ejecuta una vez al iniciar el servidor)
27
+ indices: Dict[str, VectorStoreIndex] = {}
28
+ for source, rel_path in SOURCES.items():
29
+ full_path = os.path.join(DOCUMENTS_BASE_PATH, rel_path)
30
+ if os.path.exists(full_path):
31
+ documents = SimpleDirectoryReader(full_path).load_data()
32
+ indices[source] = VectorStoreIndex.from_documents(documents)
33
+
34
+ # Recurso din谩mico para b煤squeda
35
+ @mcp.resource(
36
+ uri="retriever://documentos/{fuente}",
37
+ name="DocumentRetriever",
38
+ description="Retrieve documents from differente regulations using semantic search.",
39
+ mime_type="application/json",
40
+ tags={"llm", "retrieval"}
41
+ )
42
+ def retrieve_docs(fuente: str, query: str, top_k: int = 3) -> dict:
43
+ """
44
+ Parameters:
45
+ fuente: Source name (for example: oms).
46
+ query: Search query.
47
+ top_k: Number of results to return.
48
  """
49
+ if fuente not in indices:
50
+ return {"error": f"Fuente '{fuente}' no disponible. Opciones: {list(indices.keys())}"}
 
51
 
52
+ retriever = indices[fuente].as_retriever(similarity_top_k=top_k)
53
+ nodes = retriever.retrieve(query)
 
54
 
55
+ results = [
56
+ {
57
+ "content": node.get_content(),
58
+ "metadata": node.metadata,
59
+ "score": node.score
 
 
 
60
  }
61
+ for node in nodes
62
+ ]
63
+
64
+ return {"results": results}
65
+
66
+
67
+
68
 
69
 
70
  @mcp.tool()