File size: 2,012 Bytes
140fcff
25e0e45
f379bec
25e0e45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f379bec
25e0e45
 
 
 
 
 
 
 
 
 
 
f379bec
25e0e45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69aae5c
25e0e45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import gradio as gr
from duckduckgo_search import DDGS

# Tool 1: Knowledge Base Search
def search_knowledge_base(problem: str) -> list[str]:
    problem = problem.lower()
    KB = {
        "wifi not working": [
            "Check if the router is on.",
            "Restart the router.",
            "Check if your device is connected to the right network."
        ],
        "computer is slow": [
            "Close unused programs.",
            "Run antivirus scan.",
            "Restart the computer.",
            "Update your OS."
        ]
    }
    for key in KB:
        if key in problem:
            return KB[key]
    return ["No matching solution found in the knowledge base."]

# Tool 2: Web Search
def search_web(query: str) -> list[dict]:
    results = []
    with DDGS() as ddgs:
        for r in ddgs.text(query, max_results=3):
            results.append({
                "title": r["title"],
                "body": r["body"],
                "href": r["href"]
            })
    return results

# Tool 3: Format response
def format_response(text: str) -> str:
    return f"🛠️ Suggested Steps:\n{text}"

# Combine tools into a single unified function
def toolbox(tool_selector, input_text):
    if tool_selector == "search_knowledge_base":
        return search_knowledge_base(input_text)
    elif tool_selector == "search_web":
        return search_web(input_text)
    elif tool_selector == "format_response":
        return format_response(input_text)
    else:
        return "Invalid tool selected."

# Define Interface
demo = gr.Interface(
    fn=toolbox,
    inputs=[
        gr.Dropdown(
            ["search_knowledge_base", "search_web", "format_response"],
            label="Tool Selector"
        ),
        gr.Textbox(label="Input Text")
    ],
    outputs=gr.JSON(label="Output"),
    title="Troubleshooting MCP Server",
    description="Select a tool and provide input."
)

# Launch with MCP server enabled
if __name__ == "__main__":
    demo.launch(mcp_server=True)