
    i                     0    d dl Z d dlZd dlmZ dedefdZy)    N)StructuredTooloriginal_toolreturnc                       fdfd}t        j                  | j                   j                   j                        S )z
    Wraps an MCP tool so it works correctly in both sync and async contexts.
    Handles Spring AI's 'request' argument convention automatically.
    c                  ,  K   t        dd       }d}|rZt        |d      rd|j                  v }n?t        |d      rd|j                  v }n$t	        |t
              rd|j                  di       v }|rd| vrd| i} j                  |        d {   S 7 w)Nargs_schemaFmodel_fieldsrequest
__fields__
properties)getattrhasattrr	   r   
isinstancedictgetainvoke)kwargsschema
needs_wrapr   s      </opt/lhia/marcimex/agent/app/mcp_integration/tool_wrapper.py_ainvokez%create_tool_wrapper.<locals>._ainvoke   s     t<
v~.&&*=*==
.&&*;*;;
FD)&&**\2*FF
)61(F"**62222s   B
BBBc                  ,    	 t        j                          d d d fd}t        j                  |      }|j	                          |j                  d       d   rd   d   S # t        $ r t        j                   di        cY S w xY w)	N)resulterrorc                     t        j                         } t        j                  |        	 | j                   di       d<   | j                          y # t        $ r}|d<   Y d }~$d }~ww xY w# | j                          w xY w)Nr   r    )asyncionew_event_loopset_event_looprun_until_complete	Exceptionclose)new_loopexcr   	containerr   s     r   run_in_threadz@create_tool_wrapper.<locals>._invoke_sync.<locals>.run_in_thread"   sw    "113&&x0%*2*E*EhFXQWFX*YIh' NN$ ! -),Ig&- NN$s)   A 	A/ A*%A2 *A//A2 2B)target<   )timeoutr   r   r   )r   get_running_loop	threadingThreadstartjoinRuntimeErrorrun)r   r&   threadr%   r   s   `  @r   _invoke_syncz)create_tool_wrapper.<locals>._invoke_sync   s    	3$$&)-=I% %%];FLLNKKK#!((X&& 	3;;x1&122	3s   A&A, ,$BB)funcnamedescriptionr   )r   from_functionr4   r5   r   )r   r2   r   s   ` @r   create_tool_wrapperr7      s@    
3"36 ''!--!--	     )r   r+   langchain_core.toolsr   r7   r   r8   r   <module>r:      s!      /6~ 6. 6r8   