|
| 1 | +from dotenv import load_dotenv |
| 2 | +import re |
| 3 | + |
| 4 | +from mcp import stdio_client, StdioServerParameters |
| 5 | +from strands import Agent |
| 6 | +from strands.tools.mcp import MCPClient |
| 7 | +import streamlit as st |
| 8 | + |
| 9 | +def remove_html_tags(text_with_html): |
| 10 | + text_with_out_html = re.sub(r"<[^>]+>", "", text_with_html) |
| 11 | + return text_with_out_html |
| 12 | + |
| 13 | +async def stream_result(stream): |
| 14 | + result = "" |
| 15 | + placeholder = st.empty() |
| 16 | + async for chunk in stream: |
| 17 | + if 'data' in chunk: |
| 18 | + result += chunk['data'] |
| 19 | + result = remove_html_tags(result) |
| 20 | + placeholder.write(result) |
| 21 | + |
| 22 | + |
| 23 | +class KubernetesMCPAgent: |
| 24 | + def __init__(self): |
| 25 | + load_dotenv() |
| 26 | + |
| 27 | + server_params = { |
| 28 | + "command": "npx", |
| 29 | + "args": [ |
| 30 | + "-y", |
| 31 | + "kubernetes-mcp-server@latest" |
| 32 | + ], |
| 33 | + "env": { |
| 34 | + "KUBECONFIG": "k3s.yaml" |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + self.stdio_mcp_client = MCPClient(lambda: stdio_client( |
| 39 | + StdioServerParameters( |
| 40 | + **server_params |
| 41 | + ) |
| 42 | + )) |
| 43 | + |
| 44 | + with self.stdio_mcp_client: |
| 45 | + # Get the tools from the MCP server |
| 46 | + tools = self.stdio_mcp_client.list_tools_sync() |
| 47 | + |
| 48 | + # Create an agent with these tools |
| 49 | + self.agent = Agent( |
| 50 | + callback_handler=None, |
| 51 | + model="us.amazon.nova-micro-v1:0", |
| 52 | + tools=tools |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | + async def send_prompt(self, prompt): |
| 57 | + with self.stdio_mcp_client: |
| 58 | + stream = self.agent.stream_async(prompt) |
| 59 | + await stream_result(stream) |
| 60 | + |
| 61 | + |
| 62 | +kubernetes_mcp_agent = KubernetesMCPAgent() |
0 commit comments