Skip to content

feat(server): tool call support context #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

taobaorun
Copy link

@taobaorun taobaorun commented Apr 17, 2025

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
Author

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

@chenjianzeng0604
Copy link

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

@taobaorun
Copy link
Author

python sdk support context modelcontextprotocol/python-sdk#380

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.

2 participants