|
| 1 | +from mcp.server.fastmcp import FastMCP |
| 2 | +from kvirt.config import Kbaseconfig, Kconfig |
| 3 | +from urllib.request import urlopen |
| 4 | + |
| 5 | +mcp = FastMCP("kclimcp") |
| 6 | + |
| 7 | + |
| 8 | +@mcp.prompt() |
| 9 | +def prompt() -> str: |
| 10 | + """Indicates contexts of questions related to kcli""" |
| 11 | + return """You are a helpful assistant who knows everything about kcli, a powerful client and library written |
| 12 | + in Python and meant to interact with different virtualization providers, easily deploy and customize VMs or |
| 13 | + full kubernetes/OpenShift clusters. All information about kcli is available at |
| 14 | + https://github.com/karmab/kcli/blob/main/docs/index.md""" |
| 15 | + |
| 16 | + |
| 17 | +@mcp.resource("resource://kcli-doc.md") |
| 18 | +def get_doc() -> str: |
| 19 | + """Provides kcli documentation""" |
| 20 | + url = 'https://raw.githubusercontent.com/karmab/kcli/refs/heads/main/docs/index.md' |
| 21 | + return urlopen(url).read().decode('utf-8') |
| 22 | + |
| 23 | + |
| 24 | +@mcp.tool() |
| 25 | +def about_kcli() -> str: |
| 26 | + """What is kcli""" |
| 27 | + return open('about.txt').read() |
| 28 | + |
| 29 | + |
| 30 | +@mcp.tool() |
| 31 | +def list_clients() -> list: |
| 32 | + """List kcli clients/providers""" |
| 33 | + clientstable = ["Client", "Type", "Enabled", "Current"] |
| 34 | + baseconfig = Kbaseconfig() |
| 35 | + for client in sorted(baseconfig.clients): |
| 36 | + enabled = baseconfig.ini[client].get('enabled', True) |
| 37 | + _type = baseconfig.ini[client].get('type', 'kvm') |
| 38 | + if client == baseconfig.client: |
| 39 | + clientstable.append([client, _type, enabled, 'X']) |
| 40 | + else: |
| 41 | + clientstable.append([client, _type, enabled, '']) |
| 42 | + return clientstable |
| 43 | + |
| 44 | + |
| 45 | +@mcp.tool() |
| 46 | +def list_vms(client: str = None) -> list: |
| 47 | + """List kcli vms for specific client or for default one when unspecified""" |
| 48 | + return Kconfig(client).k.list() |
| 49 | + |
| 50 | + |
| 51 | +@mcp.tool() |
| 52 | +def info_vm(name: str, client: str = None) -> dict: |
| 53 | + """Get info of a kcli vm""" |
| 54 | + return Kconfig(client).k.info(name) |
| 55 | + |
| 56 | + |
| 57 | +@mcp.tool() |
| 58 | +def create_vm(name: str, profile: str, overrides: dict, client: str = None) -> dict: |
| 59 | + """Create a kcli vm""" |
| 60 | + return Kconfig(client).create_vm(name, profile, overrides=overrides) |
| 61 | + |
| 62 | + |
| 63 | +@mcp.tool() |
| 64 | +def delete_vm(vm: str, client: str = None) -> dict: |
| 65 | + """Delete a kcli vm""" |
| 66 | + return Kconfig(client).k.delete(vm) |
| 67 | + |
| 68 | + |
| 69 | +def main(): |
| 70 | + mcp.run(transport="stdio") |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + main() |
0 commit comments