Skip to content

Commit 77082dd

Browse files
committed
mcp image handling
1 parent 107def0 commit 77082dd

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

koboldcpp.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2597,6 +2597,24 @@ def strip_oaicontent_of_media(oaicontent):
25972597
return outarr
25982598
return oaicontent
25992599

2600+
def strip_mcpcontent_of_media(mcpcontentstr):
2601+
try:
2602+
if isinstance(mcpcontentstr, str):
2603+
#we try to strip out the b64 of MCP type tool responses with images for past turns
2604+
mcp_pl = json.loads(mcpcontentstr)
2605+
pl_modified = False
2606+
if isinstance(mcp_pl, dict) and isinstance(mcp_pl.get("content",None),list):
2607+
pl_arr = mcp_pl.get("content",[])
2608+
for idx in range(len(pl_arr)):
2609+
if pl_arr[idx].get("type","")=="image" and pl_arr[idx].get("data","")!="":
2610+
pl_arr[idx]["data"] = "(base64 data attached)"
2611+
pl_modified = True
2612+
if pl_modified:
2613+
mcpcontentstr = json.dumps(mcp_pl)
2614+
except Exception:
2615+
pass
2616+
return mcpcontentstr
2617+
26002618
#returns the found JSON of the correct tool to use, or None if no tool is suitable
26012619
def determine_tool_json_to_use(genparams, curr_ctx, assistant_message_start, is_followup_tool):
26022620
# tools handling: Check if user is passing a openai tools array, if so add to end of prompt before assistant prompt unless tool_choice has been set to None
@@ -2629,7 +2647,9 @@ def determine_tool_json_to_use(genparams, curr_ctx, assistant_message_start, is_
26292647
tool_call_chunk = []
26302648
for message in reversed_messages:
26312649
if message["role"] == "tool":
2632-
tool_call_chunk.append(message["content"])
2650+
toolrespstr = message["content"]
2651+
# toolrespstr = strip_mcpcontent_of_media(toolrespstr)
2652+
tool_call_chunk.append(toolrespstr)
26332653
else:
26342654
break
26352655
tmp_tool_replies = list(reversed(tool_call_chunk))
@@ -2734,6 +2754,15 @@ def sweep_media_from_messages(messages_array):
27342754
data = item.get("input_audio", {}).get("data")
27352755
if data:
27362756
audio.append(data)
2757+
elif message.get("role", "")=="tool" and isinstance(curr_content, str): #handle mcp returned images
2758+
try:
2759+
mcp_pl = json.loads(curr_content)
2760+
if isinstance(mcp_pl, dict) and isinstance(mcp_pl.get("content",None),list):
2761+
pl_arr = mcp_pl.get("content",[])
2762+
if len(pl_arr)>0 and pl_arr[0].get("type","")=="image" and pl_arr[0].get("data","")!="":
2763+
images.append(pl_arr[0].get("data",""))
2764+
except Exception:
2765+
pass
27372766
imgs_ollama = message.get("images", None)
27382767
if imgs_ollama:
27392768
for img in imgs_ollama:
@@ -2944,6 +2973,8 @@ def transform_genparams(genparams, api_format, use_jinja):
29442973
messages_string += "\n(Made a function call)\n"
29452974
pass # do nothing
29462975
elif isinstance(curr_content, str):
2976+
if latest_turn_was_tool and message_index < len(messages_array):
2977+
curr_content = strip_mcpcontent_of_media(curr_content)
29472978
messages_string += curr_content
29482979
elif isinstance(curr_content, list): #is an array
29492980
for item in curr_content:

0 commit comments

Comments
 (0)