llama-stack/distributions
Shrey 30ef1c3680
feat: Add model context protocol tools with ollama provider (#1283)
# What does this PR do?
Model context protocol (MCP) allows for remote tools to be connected
with Agents. The current Ollama provider does not support it. This PR
adds necessary code changes to ensure that the integration between
Ollama backend and MCP works.

This PR is an extension of #816 for Ollama. 

## Test Plan
[Describe the tests you ran to verify your changes with result
summaries. *Provide clear instructions so the plan can be easily
re-executed.*]

1. Run llama-stack server with the command:
```
llama stack build --template ollama --image-type conda
llama stack run ./templates/ollama/run.yaml \
  --port $LLAMA_STACK_PORT \
  --env INFERENCE_MODEL=$INFERENCE_MODEL \
  --env OLLAMA_URL=http://localhost:11434
```

2. Run the sample client agent with MCP tool:
```
from llama_stack_client.lib.agents.agent import Agent
from llama_stack_client.lib.agents.event_logger import EventLogger
from llama_stack_client.types.agent_create_params import AgentConfig
from llama_stack_client.types.shared_params.url import URL
from llama_stack_client import LlamaStackClient
from termcolor import cprint

## Start the local MCP server
# git clone https://github.com/modelcontextprotocol/python-sdk
# Follow instructions to get the env ready
# cd examples/servers/simple-tool
# uv run mcp-simple-tool --transport sse --port 8000

# Connect to the llama stack server
base_url="http://localhost:8321"
model_id="meta-llama/Llama-3.2-3B-Instruct"
client = LlamaStackClient(base_url=base_url)


# Register MCP tools
client.toolgroups.register(
    toolgroup_id="mcp::filesystem",
    provider_id="model-context-protocol",
    mcp_endpoint=URL(uri="http://localhost:8000/sse"))

# Define an agent with MCP toolgroup 
agent_config = AgentConfig(
    model=model_id,
    instructions="You are a helpful assistant",
    toolgroups=["mcp::filesystem"],
    input_shields=[],
    output_shields=[],
    enable_session_persistence=False,
)
agent = Agent(client, agent_config)
user_prompts = [
    "Fetch content from https://www.google.com and print the response"
]

# Run a session with the agent
session_id = agent.create_session("test-session")
for prompt in user_prompts:
    cprint(f"User> {prompt}", "green")
    response = agent.create_turn(
        messages=[
            {
                "role": "user",
                "content": prompt,
            }
        ],
        session_id=session_id,
    )
    for log in EventLogger().log(response):
        log.print()
```
# Documentation
The file docs/source/distributions/self_hosted_distro/ollama.md is
updated to indicate the MCP tool runtime availability.

Signed-off-by: Shreyanand <shanand@redhat.com>
2025-02-26 15:38:18 -08:00
..
bedrock Update default port from 5000 -> 8321 2025-01-16 15:26:48 -08:00
cerebras Update default port from 5000 -> 8321 2025-01-16 15:26:48 -08:00
dell-tgi More generic image type for OCI-compliant container technologies (#802) 2025-01-17 16:37:42 -08:00
fireworks [CICD] add simple test step for docker build workflow, fix prefix bug (#821) 2025-01-18 15:16:05 -08:00
meta-reference-gpu Update default port from 5000 -> 8321 2025-01-16 15:26:48 -08:00
meta-reference-quantized-gpu More generic image type for OCI-compliant container technologies (#802) 2025-01-17 16:37:42 -08:00
ollama Auto-generate distro yamls + docs (#468) 2024-11-18 14:57:06 -08:00
remote-nvidia Update default port from 5000 -> 8321 2025-01-16 15:26:48 -08:00
remote-vllm rename LLAMASTACK_PORT to LLAMA_STACK_PORT for consistency with other env vars (#744) 2025-01-10 11:09:49 -08:00
runpod Add Runpod Provider + Distribution (#362) 2025-01-23 12:19:02 -08:00
sambanova Sambanova - LlamaGuard (#886) 2025-01-27 15:46:30 -08:00
tgi Auto-generate distro yamls + docs (#468) 2024-11-18 14:57:06 -08:00
together kill old readme 2025-02-02 06:49:01 -08:00
vllm-gpu More generic image type for OCI-compliant container technologies (#802) 2025-01-17 16:37:42 -08:00
dependencies.json feat: Add model context protocol tools with ollama provider (#1283) 2025-02-26 15:38:18 -08:00