Skip to content

Commit c1225c1

Browse files
committed
more comments to explain the logic
1 parent e4c64c8 commit c1225c1

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

langchain_mcp_adapters/tools.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ async def call_tool(
137137
call_tool_result = await session.call_tool(tool.name, arguments)
138138
return _convert_call_tool_result(call_tool_result)
139139

140+
# base types being mapped from JSON
140141
type_map = {
141142
'null': None,
142143
'integer': int,
@@ -148,6 +149,15 @@ async def call_tool(
148149
}
149150

150151
def _parse_model_fields(args, injected_state):
152+
"""Parse a JSON field into a Pydantic Field, taking into account injected state
153+
154+
:param args: the function parameter schema
155+
:type args: dict
156+
:param injected_state: the name of the key used for the InjectedState
157+
:type injected_state: str
158+
:return: returns a dict of fields with their pydantic type and default value if any
159+
:rtype: dict
160+
"""
151161
model_fields = {}
152162

153163
def _parse_field(props):
@@ -174,9 +184,12 @@ def _parse_field(props):
174184
model_fields[field] = (field_type, ...)
175185
return model_fields
176186

177-
args = tool.inputSchema
187+
args = tool.inputSchema
188+
# check for the `injected_state`` annotation on the MCP tool.
189+
# The injected_state value is the name of the function parameter used as the injected state
178190
injected_state = tool.annotations.model_extra.get('injected_state')
179191
if injected_state:
192+
# import langgraph InjectedState only if we need it
180193
from langgraph.prebuilt import InjectedState
181194
model_fields = _parse_model_fields(args, injected_state)
182195

0 commit comments

Comments
 (0)