> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Deploying MCP Server KMCP Way

> Guide to scaffolding, running, debugging, building, and deploying a Python MCP server with KMCP and MCP Inspector exposing cryptocurrency price tools

Hello everyone.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/oFiUg2UW4IF2IjXJ/images/KAgent-Host-Your-AI-Agents-on-Kubernetes/KMCP-Installation-Overview/Demo-Deploying-MCP-Server-KMCP-Way/deploying-mcp-server-kmcp-demo.jpg?fit=max&auto=format&n=oFiUg2UW4IF2IjXJ&q=85&s=43ea8dde663fbe167314c77020ab0e74" alt="A presentation slide titled &#x22;Deploying MCP Server KMCP Way&#x22; with the word &#x22;Demo&#x22; on a dark curved design element to the right. The slide also shows a small &#x22;© Copyright KodeKloud&#x22; notice in the corner." width="1920" height="1080" data-path="images/KAgent-Host-Your-AI-Agents-on-Kubernetes/KMCP-Installation-Overview/Demo-Deploying-MCP-Server-KMCP-Way/deploying-mcp-server-kmcp-demo.jpg" />
</Frame>

Welcome to this hands-on lab. In this lesson we'll scaffold a Python-based MCP server using the kmcp CLI. The example MCP server will fetch real-time cryptocurrency prices and expose that functionality as MCP tools that agents can call.

High-level flow:

* An agent requests a tool (for example, "get Bitcoin price").
* The agent calls the MCP server’s tool endpoints.
* The MCP server executes the tool code (fetches live price), processes the response, and returns it.
* KMCP helps scaffold the project, run locally with the Inspector, build a container image, and deploy the MCP server to Kubernetes.

<Callout icon="lightbulb" color="#1CB2FE">
  KMCP is the CLI for building and managing Model Context Protocol (MCP) servers. Do not confuse it with the [KAgent CLI](https://learn.kodekloud.com/user/courses/kagents-host-your-ai-agents-on-kubernetes) — KAgent CLI focuses on building and interacting with agents, while KMCP focuses on MCP server tooling, scaffolding, local debugging, builds, and deployments.
</Callout>

## Install KMCP CLI and MCP Inspector

Install the MCP Inspector (GUI for testing/debugging) and the kmcp CLI.

```bash theme={null}
# Install MCP Inspector (visual debugging)
npm install -g @modelcontextprotocol/inspector

# Install kmcp CLI (official install script from GitHub)
curl -fsSL https://raw.githubusercontent.com/kagent-dev/kmcp/refs/heads/main/scripts/get-kmcp.sh | bash

# Verify kmcp installation
kmcp --version
```

Example console output after installation:

```bash theme={null}
Downloading https://github.com/kagent-dev/kmcp/releases/download/v0.2.2/kmcp-linux-amd64
Preparing to install kmcp into /usr/local/bin
kmcp installed into /usr/local/bin/kmcp

🎉 KMCP installation completed successfully!
To verify the installation, please run:
kmcp --version

controlplane ~ ➜ kmcp --version
kmcp version 0.2.2
```

To explore kmcp commands:

```bash theme={null}
kmcp --help
```

Abbreviated sample output:

```text theme={null}
KMCP is a CLI tool for building and managing Model Context Protocol (MCP) servers.

Usage:
  kmcp [flags]

Available Commands:
  add-tool     Add a new MCP tool to your project
  build        Build MCP server as a Docker image
  deploy       Deploy MCP server to Kubernetes
  init         Initialize a new MCP server project
  install      Install the KMCP controller on a Kubernetes cluster
  run          Run MCP server locally
  secrets      Manage project secrets
  help         Help about any command
```

## Initialize a Python MCP Project

Create a new Python MCP scaffold:

```bash theme={null}
kmcp init python crypto-price-mcp
```

Sample output:

```text theme={null}
To run the server locally:
  kmcp run local --project-dir /root/crypto-price-mcp
✓ Successfully created Python MCP server project: crypto-price-mcp
```

View the generated project structure:

```bash theme={null}
ls -ltr crypto-price-mcp
```

Example listing:

```text theme={null}
total 28
drwxr-xr-x 2 root root 4096 Dec 19 07:38 tests
drwxr-xr-x 4 root root 4096 Dec 19 07:38 src
-rw-r--r-- 1 root root 4918 Dec 19 07:38 README.md
-rw-r--r-- 1 root root 1042 Dec 19 07:38 pyproject.toml
-rw-r--r-- 1 root root 1824 Dec 19 07:38 Dockerfile
-rw-r--r-- 1 root root 586 Dec 19 07:38 kmcp.yaml
```

## Project Key Files

Below is a quick reference to the key files generated by `kmcp init`:

| File             | Purpose                                                  | Example / Notes                              |
| ---------------- | -------------------------------------------------------- | -------------------------------------------- |
| `kmcp.yaml`      | Main MCP configuration (environments, secrets, metadata) | See example YAML below                       |
| `pyproject.toml` | Python packaging and dependency configuration            | Use `dependency-groups` for dev dependencies |
| `Dockerfile`     | Image build instructions for containerization            | Used by `kmcp build`                         |
| `src/`           | Source code: core server and tools                       | `src/main.py`, `src/tools/*`                 |

Example `kmcp.yaml` (abbreviated):

```yaml theme={null}
name: crypto-price-mcp
framework: fastmcp-python
version: 0.1.0
description: MCP server built with fastmcp-python
secrets:
  local:
    enabled: false
    provider: env
    file: .env.local
  production:
    enabled: false
    provider: kubernetes
    secretName: crypto-price-mcp-secrets-production
    namespace: default
created_at: 2025-12-19T07:38:32.303583657Z
updated_at: 2025-12-19T07:38:32.338228897Z
```

<Callout icon="warning" color="#FF6B6B">
  Do not commit sensitive keys or API secrets to version control. Use `kmcp secrets` with a secrets provider (env files or Kubernetes Secrets) and reference them from `kmcp.yaml` so your tools can read them securely at runtime.
</Callout>

## Inspecting src/main.py

The generated `src/main.py` boots the FastMCP server and registers tools. It typically parses CLI arguments for transport and host/port, then starts the server.

Representative excerpt:

```python theme={null}
# src/main.py
import argparse
import sys
from pathlib import Path

# Add src to Python path
sys.path.insert(0, str(Path(__file__).parent))

from core.server import DynamicMCPServer  # noqa: E402

def main() -> None:
    """Main entry point for the MCP server."""
    parser = argparse.ArgumentParser(description="crypto-price-mcp MCP Server")
    parser.add_argument("--transport", choices=["stdio", "http"], default="stdio",
                        help="Transport mode: stdio or http")
    parser.add_argument("--host", default="127.0.0.1", help="Host for HTTP transport")
    parser.add_argument("--port", type=int, default=8080, help="Port for HTTP transport")
    args = parser.parse_args()

    server = DynamicMCPServer(project_dir=Path(__file__).parent)
    server.run(transport=args.transport, host=args.host, port=args.port)

if __name__ == "__main__":
    main()
```

Notes:

* `DynamicMCPServer` loads tools from `src/tools` and exposes them over the chosen transport.
* Use `--transport http` for running as an HTTP service (suitable for Kubernetes), or `stdio` for Inspector/local development.

## Tools: Example echo tool

Tools live under `src/tools`. Each tool is a Python function decorated with `@mcp.tool()` so the MCP server can register it and expose metadata.

Example `src/tools/echo.py`:

```python theme={null}
# src/tools/echo.py
"""Example echo tool for crypto-price-mcp MCP server."""

from core.server import mcp
from core.utils import get_tool_config

@mcp.tool()
def echo(message: str) -> str:
    """Echo a message back to the client.

    Args:
        message: The message to echo

    Returns:
        The echoed message with any configured prefix
    """
    config = get_tool_config("echo")
    prefix = config.get("prefix", "")
    return f"{prefix}{message}" if prefix else message
```

Tool definition tips:

* `@mcp.tool()` registers the function as a callable MCP tool.
* Type annotations help schema generation for inputs/outputs.
* Docstrings are surfaced in the Inspector UI as descriptions.
* Use `core.utils.get_tool_config` to read tool-specific configuration from `kmcp.yaml`.

## Add a New Tool and Run Locally

Add a new tool scaffold to your project:

```bash theme={null}
kmcp add-tool my_tool --project-dir ./crypto-price-mcp
```

Run the MCP server locally. The kmcp run command installs dependencies and starts the MCP Inspector proxy for local debugging:

```bash theme={null}
kmcp run --project-dir ./crypto-price-mcp
```

Example output when starting:

```text theme={null}
warning: The `tool.uv.dev-dependencies` field (used in pyproject.toml) is deprecated; use `dependency-groups.dev` instead
Resolved 104 packages
Starting MCP inspector...
⚙ Proxy server listening on 127.0.0.1:6277
🔑 Session token: 46f50eb8951e5e501c94fffdec7a4dd39a1973a4c05c737e2e32e76aeed590a3
Use this token to authenticate requests

🔗 Open inspector with token pre-filled:
http://localhost:6274/?MCP_PROXY_AUTH_TOKEN=46f50eb8951e5e501c94fffdec7a4dd39a1973a4c05c737e2e32e76aeed590a3

🟢 MCP Inspector is up and running at http://127.0.0.1:6274 🚀
```

## MCP Inspector — Visual Debugging and Testing

MCP Inspector is a Postman-like UI for exploring and invoking tools exposed by an MCP server. It proxies connections to the running server and displays tools, schemas, and responses.

Example inspector startup output:

```text theme={null}
Starting MCP inspector...
Proxy server listening on 127.0.0.1:6277
Session token: d2e3a6e837cd46fbd3bf344186d490d9fb1e0808e0b9a6554afcd49fb5d8f04a

Open inspector with token pre-filled:
http://localhost:6274/?MCP_PROXY_AUTH_TOKEN=d2e3a6e837cd46fbd3bf344186d490d9fb1e0808e0b9a6554afcd49fb5d8f04a

MCP Inspector is up and running at http://127.0.0.1:6274 🚀
Connection details saved to: /root/mcp-inspector-info.txt
```

### Connecting the Inspector to the MCP Server

In the Inspector UI, configure the connection:

* Transport Type: STDIO
* Command: `uv` (the uv entrypoint used in the generated project)
* Arguments: `run python /root/crypto-price-mcp/src/main.py`

Example inspector connection command shown in the UI:

```text theme={null}
Command: uv
Arguments: run python /root/crypto-price-mcp/src/main.py
```

Provide the Inspector Proxy Address and Session Token (from the start output) and click Connect. When connected you'll see a green indicator and can list and invoke tools.

### Listing and Running Tools in Inspector

The Inspector lists available tools and their input/output schemas. For the scaffolded project you'll see the `echo` tool.

Inspector tools listing example:

```text theme={null}
Tools
List Tools

echo
Echo a message back to the client.
Args: message: The message to echo
Returns: The echoed message with any configured prefix
```

Run the `echo` tool by providing input and clicking Run. Example response shown by Inspector:

```json theme={null}
{
  "result": "hello how are you today ?"
}
```

Under the hood: the Inspector sends an MCP request to the server; the server validates and dispatches to the registered tool function; the tool runs, and the server returns an MCP-compliant response the Inspector displays.

## Build and Deploy (Overview)

Once your tools are ready, build a Docker image and deploy to Kubernetes:

```bash theme={null}
# Build Docker image (tag as required)
kmcp build --project-dir ./crypto-price-mcp -t myregistry/crypto-price-mcp:latest

# Deploy using the generated kmcp.yaml and image
kmcp deploy --file crypto-price-mcp/kmcp.yaml --image myregistry/crypto-price-mcp:latest
```

Explore `kmcp --help` for command options for init, add-tool, run, build, deploy, install, and secrets management.

## Next Steps / Recommendations

* Implement a real crypto pricing tool under `src/tools/` that calls a public API for BTC/ETH prices.
* Store API keys and secrets via `kmcp.yaml` and a secrets provider; fetch them in tools with `core.utils.get_tool_config`.
* Iterate rapidly using MCP Inspector during development to validate tool schemas and responses.
* When satisfied, run `kmcp build` to produce an image and `kmcp deploy` to push to Kubernetes.
* Read the KMCP repository and docs for advanced deployment options and controllers.

Links and references:

* [KMCP repository (GitHub)](https://github.com/kagent-dev/kmcp)
* [MCP Inspector npm package](https://www.npmjs.com/package/@modelcontextprotocol/inspector)
* [KAgent CLI course (context)](https://learn.kodekloud.com/user/courses/kagents-host-your-ai-agents-on-kubernetes)

Good luck building your custom MCP server!

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/kagents-host-your-ai-agents-on-kubernetes/module/e50e4420-d5bb-46a9-b9fd-b1e827a675dc/lesson/2ed12c2a-b9a8-43be-8272-cb1e782e655a" />
</CardGroup>
