MCP
MCP : 4D.MCPContext
| Parameter | Type | Description | |
|---|---|---|---|
| Function result | 4D.MCPContext | ← | Context of the current MCP tool call |
History
| Release | Changes |
|---|---|
| 21 R6 | Created |
Description
The MCP command returns the 4D.MCPContext object of the current call of a tool of the MCP server.
The 4D.MCPContext object gives information about the call (.toolName, .clientInfo, .canElicit) and lets the tool:
- ask the user for a confirmation or for values before running an action (
.requireInput(),.hasInput(),.input,.bound), - return an error to the client (
.error()).
The context is available while an exposed MCP Function runs as a tool of the MCP server, including in the methods and functions it calls.
Outside an MCP tool call (for example in a REST request or in a method run from the 4D environment), MCP returns an inert object:
.hasInput()returns False,.requireInput()asks nothing and returns Null,.error()does nothing,.toolNameis an empty string,.clientInfois Null, and.canElicitis False.
For more information, see Asking the user for confirmation.
Example 1
You want the user to confirm the deletion of an order before it is dropped:
// cs.OrderEntity class
Class extends Entity
// Deletes the order
exposed MCP Function delete() : Text
If (Not(MCP.hasInput("confirm")))
MCP.requireInput({message: "Delete order "+String(This.ID)+"?"; confirm: {type: "boolean"; title: "Delete"}}; This.getStamp())
return
End if
If (MCP.bound#This.getStamp())
MCP.error("The order was modified, please try again")
return
End if
If (MCP.input.confirm)
This.drop()
return "Order deleted"
End if
return "Deletion cancelled"
Example 2
A method called both by an MCP tool and by other code checks whether it runs in an MCP tool call:
var $context : 4D.MCPContext:=MCP
If ($context.toolName="")
// not called from an MCP tool: $context is inert
End if
See also
MCPContext class
MCP server
Session
Properties
| Command number | 1872 |
| Thread safe | yes |