Skip to content
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

fix: Correctly structure tool responses #396

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

apappascs
Copy link

Refactored list_issues, update_issue, add_issue_comment, and get_issue tools to return their responses in the correct MCP content structure following the spec https://github.com/modelcontextprotocol/specification/blob/main/schema/schema.json#L80

--
tested with spring ai mcp client:

package org.springframework.ai.mcp.samples.github;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.mcp.client.McpClient;
import org.springframework.ai.mcp.client.McpSyncClient;
import org.springframework.ai.mcp.client.stdio.ServerParameters;
import org.springframework.ai.mcp.client.stdio.StdioServerTransport;
import org.springframework.ai.mcp.spring.McpFunctionCallback;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

	@Bean
	public CommandLineRunner readRepositoryOperations(ChatClient.Builder chatClientBuilder,
			McpSyncClient mcpClient,
			ConfigurableApplicationContext context) {
		return args -> {
			var chatClient = chatClientBuilder
					.defaultFunctions(mcpClient.listTools(null)
							.tools()
							.stream()
							.map(tool -> new McpFunctionCallback(mcpClient, tool))
							.toArray(McpFunctionCallback[]::new))
					.build();


			// 1. list_issues Command (Read Operation on `modelcontextprotocol/servers`):
			String listIssuesCommand = "List the first 10 issues from the repository 'modelcontextprotocol/servers'.";
			System.out.println("COMMAND: " + listIssuesCommand);
			System.out.println("RESPONSE: " + chatClient.prompt(listIssuesCommand).call().content());

			// 2. get_issue Command (Read Operation on `modelcontextprotocol/servers`):
			String getIssueCommand = "Get the details of issue number 10 from the repository 'modelcontextprotocol/servers'.";
			System.out.println("COMMAND: " + getIssueCommand);
			System.out.println("RESPONSE: " + chatClient.prompt(getIssueCommand).call().content());

			// 3. Create Issue Command (Write Operation on `apappascs/test`)
			String createIssueCommand = "Create an issue in the repository 'apappascs/test' with the title 'Test Issue via MCP' and the body 'This is a test issue created using MCP.'";
			System.out.println("COMMAND: " + createIssueCommand);
			System.out.println("RESPONSE: " + chatClient.prompt(createIssueCommand).call().content());

			// 4. update_issue Command (Write Operation on `apappascs/test`):
			String updateIssueCommand = "Update issue number 1 in the repository 'apappascs/test' setting title to 'Updated Title via MCP' and the state to closed.";
			System.out.println("COMMAND: " + updateIssueCommand);
			System.out.println("RESPONSE: " + chatClient.prompt(updateIssueCommand).call().content());

			// 5. add_issue_comment Command (Write Operation on `apappascs/test`):
			String addIssueCommentCommand = "Add the comment 'This is a test comment using MCP' to issue number 1 in the repository 'apappascs/test'.";
			System.out.println("COMMAND: " + addIssueCommentCommand);
			System.out.println("RESPONSE: " + chatClient.prompt(addIssueCommentCommand).call().content());

			context.close();
		};
	}

	@Bean(destroyMethod = "close")
	public McpSyncClient mcpClient() {

		var stdioParams = ServerParameters.builder("node")
				.args("/Users/alex/test_workspace/ai_projects/servers/src/github/dist/index.js")
				.addEnvVar("GITHUB_PERSONAL_ACCESS_TOKEN", System.getenv("GITHUB_PERSONAL_ACCESS_TOKEN"))
				.build();

		// Create the MCP client
		var mcpClient = McpClient.sync(new StdioServerTransport(stdioParams));

		// Initialize the MCP client and log the status
		var initStatus = mcpClient.initialize();
		System.out.println("MCP Initialized: " + initStatus);

		return mcpClient;
	}
}

Refactored list_issues, update_issue, add_issue_comment, and get_issue
tools to return their responses in the correct MCP `content` structure following the spec https://github.com/modelcontextprotocol/specification/blob/main/schema/schema.json#L80
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.

1 participant