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