Skip to content

Commit afd038f

Browse files
committed
feat(types): introduce MCP definitions and align with latest OpenAI spec
- Add comprehensive Model Context Protocol (MCP) type definitions, including `MCPTool`, `MCPToolCall`, `MCPListTools`, connector IDs, and approval filters to support remote server tool calling. - Add `ServiceTier` literal ("auto", "default", etc.) and include the `service_tier` field in `CreateChatCompletionResponse`. - Restrict `finish_reason` in completion responses to strict standard literals (`stop`, `length`, `tool_calls`, `content_filter`, `function_call`). - Introduce `ChatCompletionMessageCustomToolCall` to support custom tool calls generated by the model. - Update `ChatCompletionRequestAssistantMessage` to include the `name` field and add descriptive docstrings to message types. Signed-off-by: JamePeng <jame_peng@sina.com>
1 parent dbb7407 commit afd038f

1 file changed

Lines changed: 110 additions & 3 deletions

File tree

llama_cpp/llama_types.py

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,18 @@ class ChatCompletionResponseChoice(TypedDict):
144144
index: int
145145
message: "ChatCompletionResponseMessage"
146146
logprobs: Optional[ChatCompletionLogprobs]
147-
finish_reason: Optional[str]
147+
finish_reason: Optional[Literal["stop", "length", "tool_calls", "content_filter", "function_call"]]
148+
149+
150+
ServiceTier = Literal["auto", "default", "flex", "scale", "priority"]
148151

149152

150153
class CreateChatCompletionResponse(TypedDict):
151154
id: str
152155
object: Literal["chat.completion"]
153156
created: int
154157
model: str
158+
service_tier: NotRequired[ServiceTier]
155159
choices: List["ChatCompletionResponseChoice"]
156160
usage: CompletionUsage
157161

@@ -300,27 +304,130 @@ class ChatCompletionRequestUserMessage(TypedDict):
300304
content: Optional[Union[str, List[ChatCompletionRequestMessageContentPart]]]
301305

302306

307+
# Function tool call
308+
303309
class ChatCompletionMessageToolCallFunction(TypedDict):
310+
"""The function that the model called."""
304311
name: str
305312
arguments: str
306313

307314

308315
class ChatCompletionMessageToolCall(TypedDict):
316+
"""A call to a function tool created by the model."""
309317
id: str
310318
type: Literal["function"]
311319
function: ChatCompletionMessageToolCallFunction
312320

321+
# Custom tool call
313322

314-
ChatCompletionMessageToolCalls = List[ChatCompletionMessageToolCall]
323+
class ChatCompletionMessageCustomToolCallCustom(TypedDict):
324+
"""The custom tool that the model called."""
325+
name: str
326+
input: str
315327

328+
class ChatCompletionMessageCustomToolCall(TypedDict):
329+
"""A call to a custom tool created by the model."""
330+
id: str
331+
type: Literal["custom"]
332+
custom: ChatCompletionMessageCustomToolCallCustom
316333

317-
class ChatCompletionRequestAssistantMessageFunctionCall(TypedDict):
334+
# The tool calls generated by the model, such as function calls.
335+
ChatCompletionMessageToolCalls = Union[
336+
ChatCompletionMessageToolCall,
337+
ChatCompletionMessageCustomToolCall
338+
]
339+
340+
341+
# MCP ToolCall
342+
343+
MCPConnectorID = Literal[
344+
"connector_dropbox",
345+
"connector_gmail",
346+
"connector_googlecalendar",
347+
"connector_googledrive",
348+
"connector_microsoftteams",
349+
"connector_outlookcalendar",
350+
"connector_outlookemail",
351+
"connector_sharepoint"
352+
]
353+
354+
MCPToolCallStatus = Literal["in_progress", "completed", "incomplete", "calling", "failed"]
355+
356+
class MCPToolCall(TypedDict):
357+
"""An invocation of a tool on an MCP server."""
358+
type: Literal["mcp_call"]
359+
id: str
360+
server_label: str
318361
name: str
362+
arguments: str # JSON string
363+
output: NotRequired[Optional[str]]
364+
error: NotRequired[Optional[str]]
365+
status: NotRequired[MCPToolCallStatus]
366+
approval_request_id: NotRequired[Optional[str]]
367+
368+
369+
class MCPListToolsTool(TypedDict):
370+
"""A tool available on an MCP server."""
371+
name: str
372+
description: Optional[str]
373+
input_schema: Dict[str, Any] # The JSON schema describing the tool's input
374+
annotations: Optional[Dict[str, Any]]
375+
376+
377+
class MCPListTools(TypedDict):
378+
"""A list of tools available on an MCP server."""
379+
type: Literal["mcp_list_tools"]
380+
id: str
381+
server_label: str
382+
tools: List[MCPListToolsTool]
383+
error: Optional[str]
384+
385+
386+
class MCPToolFilter(TypedDict):
387+
"""A filter object to specify which tools are allowed."""
388+
tool_names: NotRequired[List[str]]
389+
read_only: NotRequired[bool]
390+
391+
392+
class MCPToolApprovalFilter(TypedDict, total=False):
393+
"""Specify which of the MCP server's tools require approval based on filters."""
394+
always: MCPToolFilter
395+
never: MCPToolFilter
396+
397+
398+
class MCPTool(TypedDict):
399+
"""
400+
Give the model access to additional tools via remote Model Context Protocol (MCP) servers.
401+
"""
402+
# The type of the MCP tool. Always `mcp`.
403+
type: Literal["mcp"]
404+
# A label for this MCP server, used to identify it in tool calls.
405+
server_label: str
406+
# The URL for the MCP server. One of `server_url` or `connector_id` must be provided.
407+
server_url: NotRequired[str]
408+
connector_id: NotRequired[MCPConnectorID]
409+
authorization: NotRequired[str]
410+
server_description: NotRequired[str]
411+
headers: NotRequired[Optional[Dict[str, str]]]
412+
# List of allowed tool names or a filter object.
413+
allowed_tools: NotRequired[Optional[Union[List[str], MCPToolFilter]]]
414+
# Specify which of the MCP server's tools require approval.
415+
require_approval: NotRequired[Optional[Union[Literal["always", "never"], MCPToolApprovalFilter]]]
416+
# Whether this MCP tool is deferred and discovered via tool search.
417+
defer_loading: NotRequired[bool]
418+
419+
420+
# Assistant message
421+
422+
class ChatCompletionRequestAssistantMessageFunctionCall(TypedDict):
319423
arguments: str
424+
name: str
320425

321426

322427
class ChatCompletionRequestAssistantMessage(TypedDict):
428+
"""Messages sent by the model in response to user messages."""
323429
role: Literal["assistant"]
430+
name: Optional[str]
324431
content: NotRequired[Optional[str]]
325432
refusal: NotRequired[Optional[str]]
326433
tool_calls: NotRequired[ChatCompletionMessageToolCalls]

0 commit comments

Comments
 (0)