does it work with ollama? are there any code examples? #1556
fahadshery
started this conversation in
General
Replies: 3 comments
-
|
Hi @fahadshery, are you looking for Python or .NET samples? |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
I’m interested in setting up Ollama, or using a mix of offline and online models based on the query and MCP tool integration. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Here is a simple implementation of using Ollama with MCP tools, assuming you are using Python. To use Ollama, access its OpenAI compatible endpoint using OpenAIChatClient. import asyncio
import os
from agent_framework import (
ChatAgent,
ChatMessage,
MCPStreamableHTTPTool,
Role,
TextContent,
)
from agent_framework.openai import OpenAIChatClient
from dotenv import load_dotenv
load_dotenv()
async def main() -> None:
async with MCPStreamableHTTPTool(
name="docker_mcp", url="http://localhost:8080/mcp"
) as mcp_server:
# You can specify the tools you want exposed by the docker mcp gateway.
mcp_server.allowed_tools = ["convert_time", "get_current_time"]
chat_client = OpenAIChatClient(
model_id="qwen3:4b",
# Ollama OpenAI compatible endpoint.
base_url="http://localhost:11434/v1/",
# Key from .env file for online models.
api_key=os.getenv("OLLAMA_API_KEY", ""),
)
agent = ChatAgent(
chat_client=chat_client,
instructions="You are a helpful assistant with access to the current time.",
name="Assistant",
tools=mcp_server, # Pass mcp_server as tools.
)
user_message = ChatMessage(
Role.USER,
contents=[TextContent("What time is it in New York City, New York?")],
)
response = await agent.run(user_message)
print(response.text)
if __name__ == "__main__":
asyncio.run(main()) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
As the title suggests, does it work with Ollama? Is there an example?
Cheers,
Beta Was this translation helpful? Give feedback.
All reactions