Skip to content

feat(server): tool call support context#172

Open
taobaorun wants to merge 2 commits into
modelcontextprotocol:mainfrom
taobaorun:feature/feature_call_tool_context
Open

feat(server): tool call support context#172
taobaorun wants to merge 2 commits into
modelcontextprotocol:mainfrom
taobaorun:feature/feature_call_tool_context

Conversation

@taobaorun

@taobaorun taobaorun commented Apr 17, 2025

Copy link
Copy Markdown

Motivation and Context

Current call tool flow can't get some information about the request.for example the header value

  • handle with context (map)
  • context has the current original request. e.g. ServerRequest
  • has the current sessionId

if has the context,the tool method can get more information.

 @Tool(description = "Get the current date and time in the user's timezone")
        String getCurrentDateTime(@ToolParam(required = false) ToolContext context) {
        context.getContext().get("sessionId")
        return LocalDateTime.now().atZone(LocaleContextHolder.getTimeZone().toZoneId()).toString();
    }
public class RequestContext {

	/**
	 * the original request object
	 */
	private Object request;

	private final Map<String, Object> context;

	public RequestContext(Map<String, Object> context) {
		this.context = Collections.unmodifiableMap(context);
	}

	public Map<String, Object> getContext() {
		return context;
	}

	public Object getRequest() {
		return request;
	}

	public void setRequest(Object request) {
		this.request = request;
	}

}

How Has This Been Tested?

Worked in the real application

Breaking Changes

  • SyncToolSpecification AsyncToolSpecification definition with the context

the request record wrapper the params and context

/**
 * @param params the parameters of the request.
 * @param context the request context
 */
public record McpRequest(Object params, RequestContext context) {
}
//before
new McpServerFeatures.SyncToolSpecification(tool, (exchange, params) -> {
          ...
        });

//after
 new McpServerFeatures.SyncToolSpecification(tool, (exchange, request) -> {
          ...
        });

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

@taobaorun

Copy link
Copy Markdown
Author

@tzolov Please review this PR for this situation and see if there’s a better way to solve it? Thanks

@chenjianzeng0604

Copy link
Copy Markdown

I also have this demand. When can this demand be supported?

@taobaorun

Copy link
Copy Markdown
Author

python sdk support context modelcontextprotocol/python-sdk#380

@He-Pin

He-Pin commented May 7, 2025

Copy link
Copy Markdown
Contributor

I will need this too.

@PascalYan

Copy link
Copy Markdown

同一个世界,同一个问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants