Skip to content

Latest commit

 

History

History
62 lines (52 loc) · 3.53 KB

File metadata and controls

62 lines (52 loc) · 3.53 KB

Prime Sequences

The primeSequence function generates four types of prime sequences:

  1. Balanced: Each prime is the arithmetic mean of the nearest prime numbers above and below it.
  2. Eisenstein: All the primes congruent to 2 modulo 3 with zero imaginary part. (3n + 2)
  3. Gaussian: All the primes congruent to 3 modulo 4 with zero imaginary part. (4n + 3)
  4. Isolated: Not part of a twin-prime pair. Neither (p-2) nor (p+2) is prime.

primeSequence has two inputs and one output:

function seq = primeSequence(n,type)
% n   : Number of primes in the generated sequence
% type: Type of the sequence -- a string type name
% seq : The primes in the requested sequence

Package and Deploy Prime Sequence Tool

Create an MCP tool from the primeSequence function. If the returned sequences are likely to be large in production, a wrapper function with URL-based output will improve performance when interacting with an AI. But for these examples, which are running without AI, a wrapper of None is sufficient. This code assumes an active MATLAB Production Server server running at localhost:9910.

prodserver.mcp.build creates the deployable archive and prodserver.mcp.deploy uploads it to the MATLAB Production Server instance running at the given endpoint. prodserver.mcp.build automatically generates the MCP tool definition; tool definitions are always required, even when wrapper functions are not. The file primeSequence.json in the example folder contains the automatically generated MCP tool definition.

ctf = prodserver.mcp.build("primeSequence",wrapper="None");
endpoint = prodserver.mcp.deploy(ctf,"localhost",9910);

Verify Deployment

Deploying the CTF archive to MATLAB Production Server makes the MCP tool available to MCP clients. Verify the deployment by checking that the server is responsive and that it hosts the expected tool.

To check if the server will respond, send an HTTP GET request to the ping endpoint. Expect a pong response.

ping = "http://localhost:9910/pprimeSequence/ping";
opts = weboptions(Timeout=180);
pong = webread(ping,opts)

To check that the server hosts the expected tool, use prodserver.mcp.exist:

tf = prodserver.mcp.exist(endpoint, "primeSequence", "Tools")

If both of these tests are successful, you may test the tool's functionality -- that is, determine if it produces the expected results.

Test Tool using MCP Protocol

The MCP protocol requires three steps to invoke a tool:

  1. Initialize the MCP server.
  2. Obtain the signature of the tool.
  3. Invoke the tool as specified by the signature.

The prodserver.mcp.call function performs these steps according to the MCP protocol. Therefore, if the function returns the expected result, you can be confident that other MCP clients will be able to successfully use this MCP tool.

Test the primeSequence tool by generating two types of prime sequences. Verify that these sequences are correct by comparing them to the sequences generated by running the original primeSequence function.

Generate the first 11 balanced primes:

bpr = prodserver.mcp.call(endpoint, "primeSequence", 11, "balanced")'
    bpr = 1×17
        5    53   157   173   211   257   263   373   563   593   607   

Generate the first 9 Eisenstein primes:

epr = prodserver.mcp.call(endpoint, "primeSequence", 9, "eisenstein")'
    epr = 1×9
        2     5    11    17    23    29    41    47    53