Skip to content

How about use decorators to simplify the code. #177

@liudonghua123

Description

@liudonghua123

Hi, I used python version of fastmcp a lot, and the python code is simple and clean. How about rewrite the following code to use decorators and doc comments.

server.addTool({
  name: "add",
  description: "Add two numbers",
  parameters: z.object({
    a: z.number(),
    b: z.number(),
  }),
  execute: async (args) => {
    return String(args.a + args.b);
  },
});

Version 1:

@server.tool({
  name: "add",
  description: "Add two numbers",
  parameters: z.object({
    a: z.number(),
    b: z.number(),
  }),
})
async function add(a: number, b: number) {
    return a + b;
};

Version 2:

/**
 * add two numbers
 * @param a First number
 * @param b Second number
 * @returns addition of a and b
 */
@server.tool()
async function add(a: number, b: number) {
    return a + b;
};

Here is python version of code.

@mcp.tool
def add(a: float, b: float) -> float:
    """add two numbers."""
    return a + b

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions