Skip to content

Commit e635364

Browse files
authored
fix: Tool Response display issue #77 (#78)
* fix: fallback to raw text was using rich format (Text.from_markup). Now from json it fallbacks to markdown if enough mardown patterns detected, if not enough fallbacks to raw text (Text) * chore: bump version to 0.18.1
1 parent 03bd986 commit e635364

File tree

5 files changed

+48
-11
lines changed

5 files changed

+48
-11
lines changed

cli-package/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "ollmcp"
3-
version = "0.18.0"
3+
version = "0.18.1"
44
description = "CLI for MCP Client for Ollama - An easy-to-use command for interacting with Ollama through MCP"
55
readme = "README.md"
66
requires-python = ">=3.10"
@@ -9,7 +9,7 @@ authors = [
99
{name = "Jonathan Löwenstern"}
1010
]
1111
dependencies = [
12-
"mcp-client-for-ollama==0.18.0"
12+
"mcp-client-for-ollama==0.18.1"
1313
]
1414

1515
[project.scripts]

mcp_client_for_ollama/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""MCP Client for Ollama package."""
22

3-
__version__ = "0.18.0"
3+
__version__ = "0.18.1"

mcp_client_for_ollama/utils/tool_display.py

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
"""
55

66
import json
7+
import re
78
from rich.console import Console, Group
89
from rich.panel import Panel
910
from rich.syntax import Syntax
1011
from rich.text import Text
1112
from typing import Any
13+
from rich.markdown import Markdown
1214

1315

1416
class ToolDisplayManager:
@@ -76,21 +78,26 @@ def display_tool_response(self, tool_name: str, tool_args: Any, tool_response: s
7678

7779
args_display = self._format_json(tool_args)
7880

79-
# Try to format response as JSON if possible, otherwise display as text
81+
# Try to format response as JSON if possible, otherwise check for markdown patterns
8082
try:
8183
response_data = json.loads(tool_response)
8284
response_display = self._format_json(response_data)
83-
84-
# Both args and response are formatted - create layout with syntax highlighting
8585
header_text = Text.from_markup("[bold]Arguments:[/bold]\n\n")
8686
response_header_text = Text.from_markup("\n[bold]Response:[/bold]\n\n")
8787
panel_renderable = Group(header_text, args_display, response_header_text, response_display)
8888

8989
except (json.JSONDecodeError, TypeError, ValueError):
90-
# Response is not JSON - display as text
90+
# Response is not JSON - check if it has enough markdown patterns
91+
markdown_count = self._count_markdown_patterns(tool_response)
92+
if markdown_count > 7: # Arbitrary threshold for markdown patterns
93+
response_display = Markdown(tool_response)
94+
else:
95+
# Not enough markdown patterns - use plain text
96+
response_display = Text(tool_response, style="white")
97+
9198
header_text = Text.from_markup("[bold]Arguments:[/bold]\n\n")
92-
response_text = Text.from_markup(f"\n[bold]Response:[/bold]\n\n[white]{tool_response}[/white]")
93-
panel_renderable = Group(header_text, args_display, response_text)
99+
response_header_text = Text.from_markup("\n[bold]Response:[/bold]\n\n")
100+
panel_renderable = Group(header_text, args_display, response_header_text, response_display)
94101

95102
self.console.print() # Add a blank line before the panel
96103
self.console.print(Panel(
@@ -101,3 +108,33 @@ def display_tool_response(self, tool_name: str, tool_args: Any, tool_response: s
101108
padding=(1, 2)
102109
))
103110
self.console.print() # Add a blank line after the panel
111+
112+
def _count_markdown_patterns(self, text: str) -> int:
113+
"""Count markdown patterns in text
114+
115+
Args:
116+
text: The text to check for markdown patterns
117+
118+
Returns:
119+
Number of markdown patterns found
120+
"""
121+
# Common markdown patterns
122+
patterns = [
123+
r'```\w*', # Code blocks with language
124+
r'```', # Code blocks without language
125+
r'^#{1,6}\s+', # Headers (# ## ### etc.)
126+
r'^\s*[-*+]\s+', # Unordered lists
127+
r'^\s*\d+\.\s+', # Ordered lists
128+
r'\*\*.*?\*\*', # Bold text
129+
r'\*.*?\*', # Italic text
130+
r'`.*?`', # Inline code
131+
r'^\s*>\s+', # Blockquotes
132+
r'\[.*?\]\(.*?\)', # Links
133+
]
134+
135+
count = 0
136+
for pattern in patterns:
137+
matches = re.findall(pattern, text, re.MULTILINE)
138+
count += len(matches)
139+
140+
return count

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "mcp-client-for-ollama"
3-
version = "0.18.0"
3+
version = "0.18.1"
44
description = "MCP Client for Ollama - A client for connecting to Model Context Protocol servers using Ollama"
55
readme = "README.md"
66
requires-python = ">=3.10"

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)